This tutorial is the fifth part of the ASP.NET Validation tutorial series. This tutorial will demonstrate how to use the custom validator for client and server side validation using ASP.NET 4.0 and C#.
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:
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:
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
To add client side validation, we will need to add in a javascript function and direct our custom validator to that function. To do this, open up Default.aspx to source mode and add in the following javascript function:
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
To test the client side validation, load up the web site and use the same data we used earlier to test the server side validation. Notice that before you get an error message on the page, the popup is displayed telling us whether or not the page is valid. This is because the javascript function we added executes before anything happens on the server.
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.
if (args.Value == "Hello World") |
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:
| TextBox1 | Result |
| Empty | 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
To add client side validation, we will need to add in a javascript function and direct our custom validator to that function. To do this, open up Default.aspx to source mode and add in the following javascript function:
function clientValidation(source, arguments) |
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
To test the client side validation, load up the web site and use the same data we used earlier to test the server side validation. Notice that before you get an error message on the page, the popup is displayed telling us whether or not the page is valid. This is because the javascript function we added executes before anything happens on the server.
No comments:
Post a Comment