This tutorial is the fourth part of the ASP.NET Validation tutorial series. This tutorial will demonstrate how to use the regular expression validator for client and server side validation using ASP.NET 4.0.
What is the RegularExpressionValidator?
The regular expressions validator is an ASP.NET control that uses a regular expression to validate data from a control. The power in this control is that you can apply any valid regular expression to it, so you can have validation for pretty much any specified input you need such as emails, ip address, zip codes, etc... Furthermore, this control also does both client and server side validation by default, however you can change it to only do server side validation if necessary.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Using the RegularExpressionValidator
To demonstrate how to use the regular expression validator, we will create a simple web site with a text box and a button on it. Then, we will configure a regular expression validator to be associated with the text box so that when the button is clicked we can determine whether or not to validate the page. To do this, create a new ASP.NET Empty Web Site and:
This has created a simple web form that will allow us to enter some text into a text box, and then click a button to check that value with the regular expression we specified for validation. The validation expression that we set checks for a series of five numbers, which corresponds to a valid 5 digit zip code. However, we can configure the validation expression to be whatever we want.
Testing
To test this out, simply load up the website and test the following values in the text box by clicking the button:
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Notice that no error was thrown when the value was empty, even though an empty value is clearly not a valid zip code. To get around this, you must apply a required field validator to the control so that it will be invalidated before it gets checked by the regular expression validator. Download the project to see more regular expression examples!
The regular expressions validator is an ASP.NET control that uses a regular expression to validate data from a control. The power in this control is that you can apply any valid regular expression to it, so you can have validation for pretty much any specified input you need such as emails, ip address, zip codes, etc... Furthermore, this control also does both client and server side validation by default, however you can change it to only do server side validation if necessary.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Using the RegularExpressionValidator
To demonstrate how to use the regular expression validator, we will create a simple web site with a text box and a button on it. Then, we will configure a regular expression validator to be associated with the text box so that when the button is clicked we can determine whether or not to validate the page. To do this, 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 regularexpressionvalidator onto the web form.
- Change the ControlToValidate property to 'TextBox1'.
- Change the ErrorMessage property to 'Input is not valid.'
- Set the ValidationExpression propert to '\d{5}'.
- Add a break line.
- Drag and drop a button onto the web form.
This has created a simple web form that will allow us to enter some text into a text box, and then click a button to check that value with the regular expression we specified for validation. The validation expression that we set checks for a series of five numbers, which corresponds to a valid 5 digit zip code. However, we can configure the validation expression to be whatever we want.
Testing
To test this out, simply load up the website and test the following values in the text box by clicking the button:
| TextBox1 | Result |
| Empty | No error, this is because no error will be returned if the value is empty. |
| 'asdfa' | Error, although the value is of length 5, they are letters not numbers. |
| 55555 | No error, this is a valid zip code. |
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Notice that no error was thrown when the value was empty, even though an empty value is clearly not a valid zip code. To get around this, you must apply a required field validator to the control so that it will be invalidated before it gets checked by the regular expression validator. Download the project to see more regular expression examples!
No comments:
Post a Comment