Validating E-Mail Addresses
One of the most common and difficult validation tasks that arise when performing form validation is validating an e-mail address. This task is actually much more difficult than you might assume because the e-mail standard is so complicated.
NOTE
You can find the specification for e-mail addresses in RFC 822, Standard for ARPA Internet Text Messages, at the following location:
ftp://ftp.rfc-editor.org/in-notes/rfc822.txt
Even if a perfect e-mail validation regular expression is beyond your grasp, however, you can still hope to validate simple e-mail addresses. For example, you can use the following regular expression to check that an e-mail address starts with one or more nonwhitespace characters, followed by an @ sign, followed by one or more nonwhitespace characters, followed by a period, followed by one or more nonwhitespace characters:
/S+@/S+/./S+
So, this regular expression would match the following e-mail addresses:
However, it would fail to match e-mail addresses like
steve@aol steve smith@aol
which is what you want.
If you want to exclude all e-mail addresses that do not end with a top-level domain name between two and three characters, such as .com, .net, and .ws, you would use this expression:
/S+@/S+/./S{2,3}
The page in Listing 3.5 demonstrates how you would use this regular expression with the RegularExpressionValidator control.
Listing 3.5 RegularExpressionValidatorEmail.aspx
<Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs ) If IsValid Then Response.Redirect( "ThankYou.aspx" ) End If End Sub </Script> <html> <head><title>RegularExpressionvalidatorEmail.aspx</title></head> <body> <form Runat="Server"> Email Address: <br> <asp:TextBox id="txtEmail" Columns="50" Runat="Server"/> <asp:RegularExpressionValidator ControlToValidate="txtEmail" Text="Invalid Email Address!" ValidationExpression="/S+@/S+/./S{2,3}" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html>
The C# version of this code can be found on the CD-ROM.
Validating Usernames and Passwords
Typically, Web sites require you to enter a username and password containing only alphanumeric characters or the underscore character. You can perform this type of validation using a regular expression that looks like this:
/w+
This regular expression matches any expression that contains one or more word characters (a word character can be a letter, number, or the underscore character).
You also can specify a minimum and maximum length for a password by using a regular expression that looks like this:
/w{8,20}
This regular expression matches only expressions that are between 8 and 20 characters long.
Finally, some Web sites require you to use at least one number and one letter in your password. You can use the following regular expression to satisfy this requirement:
[a-zA-Z]+/w*/d+/w*
This regular expression requires you to enter at least one letter, followed by any number of word characters, followed by at least one number, followed by any number of word characters.
Listing 3.6 demonstrates how you can combine two RegularExpressionValidator controls and a RequiredFieldValidator control to validate a password. The Validation controls require you to enter a password that starts with at least one letter and contains one number and between 3 and 20 characters (special characters, such as # and ?, are not allowed).