What is the CustomValidator?
The custom validator is an ASP.NET control that allows you to apply custom methods to a page to determine if it is valid or not. The custom validator is unique because it can be used to validate either one or many controls. Furthermore, the custom validator also allows you the option to use client side validation, server side validation, or both. The custom validator can be used to validate pretty much anything, but is mainly used in cases where other validators are simply not an option. The ASP.NET validation controls only allow specific controls to be validated by setting their ControlToValidate
property. In a scenario in which you need to validate a control that cannot be set to that property, you can use the custom validator to execute a custom validation method.
We moved our web sites to Server Intellect
and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Server Side Validation
First, we will demonstrate how to use the custom validator to apply server side validation to a text box. To do this, we will need to create a simple web site with a text box and button to cause validation on it. To begin, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a textbox onto the web form.
- Drag and drop a customvalidator onto the web form.
- Change the ErrorMessage property to 'Page is not valid.'
- Set the ControlToValidate property to 'TextBox1'.
- Add a break line.
- Drag and drop a button onto the web form.
This has setup a simple form with a text box that is associated with a custom validator, and a button to cause validation when clicked. The custom validator works differently from the other validators in that you must write your own validation code on the server to determine if the page is valid or not. To do this, double click the custom validator to generate the CustomValidator1_ServerValidate
event method. This method is executed when validation is caused on the page by something such as a button. Because we have a control to validate, we will be able to access the value of that control from the ServerValidateEventArgs
object named 'args' that is passed to our method. However, it is important to note that even if we didn't have a control to validate, we could access the value of any control just as easily by using the control's name. An example of this would be using 'TextBox1.Text' instead of 'args.Value' to access the value we want to validate. Let's go ahead and add in some code to only validate the page if our text box reads 'Hello World'. To do this, add the following code to the CustomValidator1_ServerValidate
if (args.Value == "Hello World")
args.IsValid = true;
args.IsValid = false;
Need help with cloud hosting
? Try Server Intellect
. We used them for our cloud hosting services
and we are very happy with the results!
This code simply checks the value we want to validate, and then sets the 'args.IsValid' property to the proper value. This allows us to have full control over our validation because we can write any code to determine whether or not to change that property. To have this validator check mutliple controls, we would simply need to add in some conditions based on the control's value and then set this property appropriately.
Testing Server Side Validation
Let's go ahead and test this out to ensure that our server side validation is working. To do this, load up the web site and test the following data in the text box by clicking the button:
|| No error, this is because the field was empty and not checked.
| Hello Validation
|| Error, the text was invalid.
| Hello World
|| No error, the text was valid.
To prevent the page from being valid when the text box is empty, you can simply apply a required field validator to it. Other than that, the validation works as intended.
Client Side Validation
function clientValidation(source, arguments)
if (arguments.Value == "Hello World")
arguments.IsValid = true;
arguments.IsValid = false;
This function does pretty much the same thing as our server validation method by checking for 'Hello Word'. Then, it will simply display a popup message that will display whether or not the page is valid.
If you're looking for a really good web host, try Server Intellect
- we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
Next, we need to configure our custom validator so it actually calls this function when validation is triggered. To do this, change the ClientValidationFunction
property of the custom validator to 'clientValidation'.
Testing Client Side Validation