特定域的Javascript电子邮件验证

时间:2010-07-07 18:51:39

标签: javascript

我无法找出遗漏的内容,以便当电子邮件有效时,它会跳过最后一条无效邮件并转到表格上的下一个项目进行验证:

enter code here 
    if (document.form1.email.value.length > 0) {

    var tst = document.form1.email.value;
    var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

    var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
    var aLst = emailRE.exec(tst)
    if (!aLst) {
        alert(tst + ' is not a valid e-mail')
    } else {
        var sLst = aLst[1].toLowerCase()
        for (var i = 0; i < okd.length; i++) {
            if (sLst == okd[i]) {
                //    alert(aLst[1] + ' is allowed');-->

           }
       }

            if (i == okd.length) alert(aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.')

            document.form1.email.select();
            return false;


    }   
}

2 个答案:

答案 0 :(得分:0)

我建议将此代码放入一个函数中,可能名为ValidateEmail()

在您的循环中:如果您确定该电子邮件有效,return true;。这将阻止进一步执行。如果该域不匹配,请让它继续循环检查其他域。

如果循环完成而没有返回true,你就会知道它与最后的return false;不匹配。

编辑:请改用try / catch:

if (document.form1.email.value.length > 0) {

    var tst = document.form1.email.value;
    var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

    try {
        var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
        var aLst = emailRE.exec(tst)

        if (!aLst)
            throw (tst + ' is not a valid e-mail');

        // isValidDomain will be changed to 'true' only if it matches an item in the array
        var isValidDomain = false;

        var sLst = aLst[1].toLowerCase()
        for (var i = 0; i < okd.length; i++) {
            if (sLst == okd[i]) {
                isValidDomain = true;
                // We break here because a match has been found - no need to compare against the other domain names.
                break;
            }
        }

        if(!isValidDomain)
            throw (aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.');

        // If execution reaches here, you know it passed both tests!
        return true;

    }
    catch(err) {

        // This code block runs whenever an error occurs

        alert(err);
        document.form1.email.select();
        return false;
    }
}

throw基本上就像goto命令一样。它将直接跳转到代码的catch(err)部分。

有关try,catch和throw的更多信息:

答案 1 :(得分:0)

非常感谢科林!

我不得不删除以下两行,以避免代码停止运行到下一个验证字段:

              isValidDomain = true;
                    // We break here because a match has been found - no need to compare against the other domain names. 
                    // break - exits code from running on down to next item on page
                }
            }

            if (!isValidDomain)
                throw (aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.');

            // If execution reaches here, you know it passed both tests! 
         //   return true; - was not needed, stops code from running on page

        }
        catch (err) {
相关问题