You can check whether user input matches the predefined mode, such as phone number, zip code, and email address. To perform this verification, you must use a regular expression.
1. Add the regularexpressionvalidator control to the page and set the following attributes:
Attribute |
Description |
Controltovalidate |
The ID of the control being verified. |
Errormessage, text, display |
These attributes specify the text and location of errors to be displayed when verification fails. |
2. Set the validationexpression attribute to a regular expression to set the mode to be compared.
To allow multiple valid modes, use the bar character (|) to separate expressions.
Note: In client verification, ecmascript (JavaScript) is used to calculate the regular expression. This is slightly different from the regular expression check on the server.
3. Add a test in ASP. NET webpage code to check the validity.
The following code example shows how to use the regularexpressionvalidator control to check whether a user has entered a valid U.S. zip code. The validators check two modes: a 5-digit number and a 5-digit number plus four additional digits.
ZIP: <asp: textbox id = "txtzip" runat = "server"> </ASP: textbox>
<Asp: regularexpressionvalidator
Id = "txtzip_validation" runat = "server"
Controltovalidate = "txtzip"
Errormessage = "enter a valid us zip code ."
Validationexpression = "/d {5} (-/d {4 })? ">
</ASP: regularexpressionvalidator>