Often a simple value or range check is
insufficient; you must check that the form of
the data entered is correct. For example, you may need to ensure that
a zip code is five digits, an email address is in the form
[email protected]
, a credit card matches the right
format, and so forth.
A regular expression validator allows you to validate that a text
field matches a regular expression
. Regular
expressions are a language for describing and manipulating text. For
more complete coverage of this topic, please see MasteringRegularExpressions
by
Jeffrey Friedl (O’Reilly).
A regular expression consists of two types of characters:
literals
and
metacharacters
. A literal is just a
character you wish to match in the target string. A metacharacter is
a special symbol that acts as a command to the regular expression
parser. The parser is the engine responsible for understanding the
regular expression. Consider this regular expression:
^d{5}$
This will match any string that has exactly 5 numerals. The initial
metacharacter, ^
, indicates the beginning of the
string. The second metacharacter, d
, indicates a
digit. The third metacharacter, {5}
, indicates
exactly 5 of the digits, and the final metacharacter,
$
, indicates the end of the string. Thus, this
regular expression matches five digits between the beginning and end
of the line, and nothing else.
A slightly more sophisticated algorithm might accept either a 5 digit
zip code or a 9 digit (plus 4) zip code in the format of 12345-1234.
Rather than using the d
metacharacter, you could
simply designate the range of acceptable values:
ValidationExpression="[0-9]{5}|[0-9]{5}-{0-9]{4}"
You create a RegularExpressionValidator much as you did the previous
validators. The only new attribute is
ValidationExpression
,
which takes a valid regular expression within quotes. For example,
the following code fragment defines a regular expression validator to
insure that the value entered into a text box is a five digit numeric
zip code:
<asp:RegularExpressionValidator ID="regExVal" ControlToValidate="txtZip" Runat="server" ValidationExpression="^d{5}$" display="Static">Please enter a valid 5 digit Zip code</asp:RegularExpressionValidator>
If the control pointed to by
ControlToValidate
has a string that matches the regular
expression, validation succeeds. The complete source is shown in
Example 8-5.
Example 8-5. Regular expression validator
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="RegularExpressionValidation.WebForm1" %> <HTML> <HEAD> <meta content="Internet Explorer 5.0" name=vs_targetSchema> <meta content="Microsoft Visual Studio 7.0" name=GENERATOR> <meta content=C# name=CODE_LANGUAGE> </HEAD> <body MS_POSITIONING="GridLayout"> <form method=post runat="server"> <table> <tr> <td colspan="2"> <h5>Please enter your Zip Code</h5> </td> </tr> <tr> <td> <asp:TextBox width="60" ID="txtZip" runat="server" /> </td> <td> <asp:RegularExpressionValidator ID="regExVal" ControlToValidate="txtZip" Runat="server" ValidationExpression="^d{5}$" display="Static">Please enter a valid 5 digit Zip code</asp:RegularExpressionValidator> </td> </tr> <tr> <td> <asp:Button ID="btnValidate" Text="Validate" Runat="server"></asp:Button> </td> <td> <asp:Label ID="lblMsg" Runat="server" Text=""/> </td> </tr> </table></FORM> </body> </HTML>
18.188.211.106