Javascript test()方法语法错误?

时间:2014-01-07 04:17:33

标签: javascript regex syntax-error

我正在尝试将(我发现的)最好的电子邮件验证功能(位于此处:http://www.linuxjournal.com/article/9585?page=0,3)从php转换为javascript。无论“你不应该使用javascript验证因为javascript可以被禁用”这一事实。显然我不能离开函数的checkdnsrr()部分,但其他一切都应该可以使用javascript。

到目前为止,该功能在此行之前按预期工作:else if(/\.\./.test(domain)) {

我知道没有上下文它就没用了,所以完整的功能如下。同样奇怪的是,它给一条具有完全相同的正则表达式模式的行“传递”:else if(/\.\./.test(local)) {在它之前用了几行。奇怪。

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;
}

1 个答案:

答案 0 :(得分:1)

您错过了之前{中的if

因此,else没有与if相关联。

相关问题