ASP.NET Multiple Field Validation - c#

In ASP.NET 2, I've used Field Validators, and RequiredField validators, but I'm unsure of how to handle a case like this.
I have two check boxes on a page, and I need to be sure that at least one of them is set. So, if you look at in binary, it can be 01, 10 or 11, but it can not be 00. My question is, what the best way to do this with checkboxes?
Can the normal ASP Validators handle this, or would I need to create an integer value like mentioned above, hidden somewhere and use a RangeValidator do a test to make sure THAT value is never zero?

Worst case you can write a CustomValidator the can do whatever you like. Sounds like what you need is along the lines of:
isValid = Check1.Checked | Check2.Checked

Use CustomValidator

This control (written by me) supports CheckBox and CheckBoxList:
http://www.codeproject.com/KB/validation/AtLeastOneOfValidator.aspx
Just add it to visual studio, drop it on your page, and add your checkboxes to it's Controls list. It will work like any other validator control.

Custom validator is the obvious solution. Also, when using a custom validator you should also check for validity on the server side just in case the javascript fails due to some reason.
P.S.: Don't always trust what the client(browser) sends you.

Here is an article I wrote regarding this exact issue. I also wanted to validate multiple controls which was easy using the CustomValidator but one of the issues I was not happy about is that if you corrected the validation issue the error did not go away until you posted back.
I figured out a way to hide the error message and revalidate and wrote a small blog entry about it. Check it out and see what you think.
http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx

Related

Disable mvc3 client-side validations under certain conditions

I am building a search form which needs multiple fields. A radio button indicates which fields are required for input like so :
[ ] Field 1
[.] Field 2
Field 3
[ ] Field 4
In the above case, Field 2 and Field 3 are now required since the associated radio button is checked. To accomplish this, I have implemented the RequiredIf validation attribute, and it works properly.
My problem are the other validations. In this case, Field 1 also has a minimum length validation. If Field 1 has any value that does not respect the minimum length validation, the form is now invalid and cannot be submitted.
I need a way to disable validation on the fields that are not required. (And also be able to set them back as another radio button is checked).
The fields cannot be set to "disabled=disabled" which solves the issue because of client requirements.
I have tried removing the data-val attributes or setting them to false on the said fields, then parsing my form again, failing miserably.
Edit: Just making sure. The problem is client-side validations.
Remember that there are 2 validations happening: client and server side. Hence, removing the data-val attribute will not help.
Now, in your models I reckon you are using [attributes] to add these validation rules. I don't think this method will not let you do conditional validations.
In this case FluentValidation can help you. http://fluentvalidation.codeplex.com/
It is quite simple to do, and you should be able to do something like:
RuleFor(model => model.Field).NotEmpty().When(model => model.FieldEnabled);
Setting the fields as "disabled" works as intended. The client has changed his mind about this requirement. Sometimes it is the best solution.
This is still open for better solutions.
Edit :
I had not thought about searching for doc on the validation plugin. There seems to be some pretty interesting methods available to use like rules ( "remove", rules ).

How to build a "word-by-word" autocomplete in WPF?

I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.

Required fields one of two fields

I am writing in asp.net c#.
I want a control similiar to RequiredFieldValidator except I want one of two fields to be required. I found an excellent example for two text fields but in my case one field is a check box and the other is an text box. If the check box is not checked the text box must be entered.
Any thoughts?
Just use javascript or C# code to check this.
I personally don't care for the RequiredFieldValidator types as they are limited and rather confusing.
With C# server side code you could just check
if (!chk.Checked && txtBox.Text.Length==0)
For JavaScript something to this effect:
if (!(document.getElementById('myCheckBox').Checked)
&& document.getElementByID('myTextBox').val=='')
You can always use the customvalidator if you have complex custom validation logic. This is probably your best way to go.

Developing a custom-validation in asp.net for specific control and criteria

There is another relevant question asked Validation Check in asp.net
In the same scenario we need a custom validator control which will alert user for any wrong entry. This will work like this :
Developer will pass the control-name, input-value and format-required
For instance like for textbox it can be: txtName,txtName.Text, allow-alphabets-only
The accordingly format if the user input is invalid he/she will be got prompt.
Please suggest the right way to do the smae.
Thanks in advance.
You've described the default behaviour of all asp.net validators (pass control to validate etc).
What you want to achive (eg, alpha charaters only, or alphanumeric only etc) can be achieved mostly with the RegularExpressionValidator.

Custom validatior in ASP.net

I'm working on an ASP.NET/C# app.
I have 2 text boxes and I need to validate if the input text for both of them are not both null
ex:
if(string.IsNullOrEmpty(TextBox1.Text) && string.IsNullOrEmpty(TextBox2.Text) )
//FAIL!!!
else
// OK!
that is, at least one txtBox has values
I was planning on using a custom validator but it seems that the validation function only gets called when something is written on the textBox i'm using has the 'control to validate'. Now, that doesn't work for me since I want to show an error message when both text boxes are empty. Is there a way to make the validation function to be called with, for example a postback? Or is there any other better approach to this case than the custom validator?
tks
If you set the 'ControlToValidate', then the validator will not fire if that control is empty. However, for a CustomValidator you can leave that empty so it will always fire.
Because the built-in validators only analyze the state of their own controls, you are forced to go with a CustomValidator for this functionality. I don't know of any better approach that still uses ASP.NET server-side validators. As Hans said, be sure to leave ControlToValidate empty, and then do the validation as in your code sample.

Categories

Resources