I'm attempting to convert (what I've found to be) the best email validation function (located here: http://www.linuxjournal.com/article/9585?page=0,3) from php to javascript. Regardless of the fact that "you shouldn't validate with javascript because javascript can be disabled". Obviously I can't leave in the checkdnsrr() portion of the function, but everything else should be doable with javascript.
So far the function works as expected up until this line:else if(/\.\./.test(domain)) {
I know it's pretty useless without context, so the full function is below. What's also weird is that it gives a "pass" to a line with the exact same regex pattern:else if(/\.\./.test(local)) { which is used a few lines before it. Strange.
function validEmail(email) {
var isValid = true;
var atIndex = email.indexOf("@");
var ending = email.length - 1;
if(typeof(atIndex) == "boolean" && !atIndex) {
isValid = false;
}
else {
var domain = email.substr(atIndex+1);
var local = email.substr(0, atIndex);
var localLen = local.length;
var domainLen = domain.length;
if(localLen < 1 || localLen > 64) {
// local part length exceeded
isValid = false;
}
else if(domainLen < 1 || domainLen > 255) {
// domain part length exceeded
isValid = false;
}
else if(local[0] == '.' || local[localLen-1] == '.') {
// local part starts or ends with '.'
isValid = false;
}
else if(/\.\./.test(local)) {
// local part has two consecutive dots
isValid = false;
}
else if(/^[A-Za-z0-9\\-\\.]+$/.test(domain) == false)
// character not valid in domain part
isValid = false;
}
else if(/\.\./.test(domain)) {
// domain part has two consecutive dots
isValid = false;
}
else if(/^(\\\\.|[A-Za-z0-9!#%&`_=\/$'*+?^{}|~.-])+$/.test(local.replace("\\\\",""))) {
// character not valid in local part unless
// local part is quoted
if(/^"(\\\\"|[^"])+"$/.test(local.replace("\\\\",""))) {
isValid = false;
}
}
}
return isValid;
}
isValid = "false";?