Following my last article on creating specialized validators, below I present the EmailValidator control which validates user input with a pattern of an email:
public class EmailValidator
: RegularExpressionValidator
{
public EmailValidator()
{
ValidationExpression =
@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@
[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.
[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
ErrorMessage = "Email is incorrect";
}
}Note that ValidationExpression is broken so it may be better displayed in the browser. As for how accurate, the expression is. It was taken from the
Regular Expression Library page and I have had no problems with it.
Usage on a page after registration:
<my:EmailValidator runat="server" ID="EmailCorrect"
ControlToValidate="Email" />