Email validation with regular expressions is way too much fun.
The code that was posted here before is still at the bottom of this page, but I have also added a newer portion.
The newer portion requires the passing of the email string instead of the matching of your forms.
-----
- New Version
-----
Remember to pass the email string that you want to validate
when you call this function:
example: validateAddress(document.enter.email.value);
function validateAddress(incoming) {
var emailstring = incoming;
var ampIndex = emailstring.indexOf("@");
var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
// find a dot in the portion of the string after the ampersand only
var dotIndex = afterAmp.indexOf(".");
// determine dot position in entire string (not just after amp portion)
dotIndex = dotIndex + ampIndex + 1;
// afterAmp will be portion of string from ampersand to dot
afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
// afterDot will be portion of string from dot to end of string
var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
var beforeAmp = emailstring.substring(0,(ampIndex));
//old regex did not allow subdomains and dots in names
//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/
// index of -1 means "not found"
if ((emailstring.indexOf("@") != "-1") &&
(emailstring.length > 5) &&
(afterAmp.length > 0) &&
(beforeAmp.length > 1) &&
(afterDot.length > 1) &&
(email_regex.test(emailstring)) ) {
return true;
} else {
alert("Please check your email address!");
return false;
}
}
------
- Old Version
------
<script type="text/javascript" language="javascript">
<!-- // hide script from old browsers
// Function to validate email address.
function validateEmail() {
var emailstring = document.enter.email.value;
var ampIndex = emailstring.indexOf("@");
var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
// find a dot in the portion of the string after the ampersand only
var dotIndex = afterAmp.indexOf(".");
// determine dot position in entire string (not just after amp portion)
dotIndex = dotIndex + ampIndex + 1;
// afterAmp will be portion of string from ampersand to dot
afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
// afterDot will be portion of string from dot to end of string
var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
var beforeAmp = emailstring.substring(0,(ampIndex));
var regex = /\;|#|\$|%|\^|&|\*|\(|\)|\+|\\|\/|\?|>|<|\{|\}|\,|\[|\]|\`|\|/;
// index of -1 means "not found"
if ((emailstring.indexOf("@") != "-1") &&
(emailstring.length > 5) &&
(afterAmp.length > 0) &&
(beforeAmp.length > 1) &&
(afterDot.length > 1) &&
! (regex.test(emailstring)) ) {
document.enter.submit();
} else {
alert("Please check your email address!");
document.enter.email.focus();
return false;
}
}
//-->
</script>