March 10, 2000 - Validating email Address
March 10, 2000 Validating email Addresses Tips: March 2000
Yehuda Shiran, Ph.D.
|
- One or more characters before the "@"
- An optional "[", because user@[255.255.255.0] is a valid e-mail
- A sequence of letters, numbers, and periods, which are all valid domain or
- IP address characters
- A period followed by a 2-3 letter suffix
- An optional "]"
Here is a function that validates this sequence:
function valid(form) {
var field = form.email; // email field
var str = field.value; // email string
var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
alert("Thank your for your feedback."); // this is optional
return true;
}
alert("\"" + str + "\" is an invalid e-mail!"); // this is also optional
field.focus();
field.select();
return false;
}
Learn more about our validation script in Column 5, JavaScript Regular Expressions.