I’m needing an email validation algorithm that works without regexp (for use with flash versions < 8, etc…) and I came across a script that I adapted a little.
Here is the script in JavaScript. Mainly what I’m looking for is people to tell me if there are any important rules that I have missed out.
Thank you:
/**
* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
* Edits, Additions and Reformatting (inc. comments) by Paul Scott (http://www.icio.co.uk/)
*/
function echeck(str) {
// Vars
var at="@";
var dot=".";
var badmail=false;
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
var chars = "+-._0123456789abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// TEST CHARACTERS
// ******************************
var i;
for (i=0; i<lstr; i++) {
if (chars.indexOf(lstr.charAt(i))==-1) {
badmail = true;
break;
}
}
// OTHER TESTS
// ******************************
// Test for an @, test that @ is not the first character, test @ is not the last character
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
badmail = true;
// Test for a ., test that . is not the first character, that . is not the last character
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
badmail = true;
// Test that there is only one @
if (str.indexOf(at,(lat+1))!=-1)
badmail = true;
// Test that there is not a . before the @, test that there is not a . after the @
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
badmail = true;
// Test that there is a . after the @ and that there is a at least 2 characters in between
if (str.indexOf(dot,(lat+3))==-1)
badmail = true;
// Test that there are no spaces
if (str.indexOf(" ")!=-1)
badmail = true;
// Test that there is a domain extension of >= 2 characters
if (lstr-str.lastIndexOf(dot)>3)
badmail = true;
return !badmail;
}