0

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;
}
6
  • 1
    What is the problem? What is the error? Commented Jan 7, 2014 at 4:18
  • isValid = "false";? Commented Jan 7, 2014 at 4:19
  • All it says it "SyntaxError: syntax error" in the console. Commented Jan 7, 2014 at 4:19
  • That incorrectly rejects IPv6 hosts. Commented Jan 7, 2014 at 4:19
  • Ah, yea sorry... That's actually not in the function I'm testing. I had a string there instead which would indicate which portion of the conditionals failed. Forgot to remove the quotes. Commented Jan 7, 2014 at 4:20

1 Answer 1

1

You missed a { in the previous if.

Therefore, that else has no if connected to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.