Tuesday, February 26, 2008

JavaScript Validate Email using RegEx

Use the code below with an asp.net custom validator to check if an email is valid.
The code below check for a value if no value is provided then is not a valid email.

The pattern below will work with gmail's email address with "+" such as johnsmith+test@gmail.com



function ValidateEmail(sender, args)
{
if(args.Value.trim().length > 0)
{
var ex = /^([a-zA-Z0-9_.+-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
var re = new RegExp(ex );

if(re.exec(args.Value))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}

}
else
{
args.IsValid = false;
}
}



No comments:

Post a Comment