Is it possible to have "Required Field Validator" controls to validate more than one field (example I have 12 textboxes that are required. I want to try an avoid having 12 RFV controls. If a validation does get triggered, is there a way to display a customized message ("textA is empty" or "textB is empty") etc.?
You can create a custom validator that goes through validates all the controls.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
The Required Field Validator can only validate one control at a time.
You cannot do that with a RequiredFieldValidator; you could write your own CustomValidator to do that, but the validation would be on the server side rather than on the client side.
Multiple Fields Validator - An ASP.NET Validation Control is what you need.
As mentioned by everyone else you can create your CustomValidator that can validate on the client side and on the server side. There are couple of things that you must keep in mind.
1) Make sure to expose your client script as a Web Resource. This will enable the script to be cached by the browser.
2) Use a certain attribute to target certain TextBoxes. This can be performed by giving them a certain class which will be validated in your Custom Validator control.
Hope it helps!
Related
I am just a bit stuck with Custom Validation. I have created a web page under which I have applied custom Validation on different fields like firstName, LastName etc...However, when I try to apply custom validation on a field which does not contains any value, the custom validation does not get fired, whether it is on Client Side or Server Side. I tried to find the answer to this as to why this was happening and I came to know that Custom Validation cannot be applied to field which does not contains any value.
However, this can be done with Required Field Validator.
I want to achieve that if First Name and Last Name are entered by the User, then they should enter Address too, making it mandatory. When I tried custom Validation on Address textbox, it does not get fired. Please help
Set the ValidateEmptyText property to true.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext.aspx
I am new to Silverlight and want to achieve validations.
I have a form with accordion control. I have controls in each section and have to have validations like Required or optional, if given a value it has to be valid like emai, phone number..... All these validations i have Dynamically enable or disable at page load.
So How i can achieve this in Silverlight 4.
In simple words how can we have Required field validator, CUstomvalidator and range validators in SilverLight.
I have gone through RIA but i am not sure how to use that on non entity fields and also Dynamically enable or disable validations.
Example i have txtFirstName, txtLastName and txtPhone. I want firstname required and Phone optional but should be validated if given some value.
All these fields are not part of any entity. Where i can add required or other attributes.
Thanks in advance.
You can add context to the validation process and allow your custom validators to be aware of some state. Fore example, you can add your page to the context and the validator can later access it to perform some conditional logic. See "RIA Services Validation: Providing ValidationContext" by Jeff Handley
How Silverlight Validation Works by Shawn Wildermuth.
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.
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.
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