【问题标题】:validating specific email address in form with javascript使用 javascript 验证表单中的特定电子邮件地址
【发布时间】:2015-04-30 13:06:07
【问题描述】:

我正在设置一个表单,我已经在其中进行了编码,以验证电子邮件表单框中是否有一个条目,如您在此处看到的那样

function checkMailing(){
//if we want to refer to the email field - which has the name 'email' - we would use the form variable (created above), as such:
//theForm.email 
//you this with the name of any field iside of the form
//alert(theForm.email.value);

//use an if statement to check the value of the form
var mailingVal = theForm.mailing.value 
mailingVal = trim(mailingVal);

if(mailingVal == "" ){
    //error message

    //add a dropshadow to the field (to highlight it)
    theForm.mailing.style.boxShadow = "0px 0px 6px #01FFFF";

    //from the form field, go up to the parent (the div with the class 'formbox', then inside of that for the div with the class 'fieldInfo', and change the text contents to be an error message
    setMessage(theForm.mailing, "error", "You must enter an address");
    /*theForm.email.parentNode.querySelector("div").innerHTML = "You must enter an email!";
    theForm.email.parentNode.querySelector("div").className = "error";*/
}else{
    //if the user entered an email (or in this anything) give them positive feedback
    theForm.mailing.style.boxShadow = "";

    setMessage(theForm.mailing, "correct", "Perfect");

    /*theForm.email.parentNode.querySelector("div").innerHTML = "Perfect)"
    theForm.email.parentNode.querySelector("div").className = "correct";*/
    }   
}

但是,我还需要它来验证它是一个特定的电子邮件地址,而不仅仅是任何电子邮件地址。例如,它必须是@gmail.com 地址,而不是@hotmail.com 或@anythingelse.com。任何指导将不胜感激!

【问题讨论】:

  • 解决方案应该是Email: <input name="mailing" type="text" />@gmail.com,我想。 (也来自 UX 经验)。那么这里就不需要任何验证了

标签: javascript html css validation email


【解决方案1】:

你可以使用正则表达式:

if (mailingVal && mailingVal.match(/@gmail\.com$/i)) {
    // it's gmail
}

【讨论】:

    【解决方案2】:

    更好的方法可能是使用正则表达式,确保要匹配的字符串以 @gmail.com 结尾

    var re = /@gmail\.com$/i; 
    
    if(re.exec(mailingVal) !== null) {
        // the test passed!
    }
    

    这将确保字符串以@gmail.com 结尾并且在.com 之后不包含任何额外字符

    使用该正则表达式,someone@gmail.com 将匹配,但 someone@gmail.comm 不会。由于/i 开关,someone@Gmail.comsomeone@GMAIL.COM(等等)也将如此。

    如果您希望它只区分大小写,只需删除 /i 开关,这样正则表达式就会像这样

    var re = /@gmail.com$/
    

    更新

    这是您代码中的正则表达式解决方案,将 exec 更改为 test(仅返回真或假,取决于正则表达式是否匹配):

    function checkMailing(){
        //if we want to refer to the email field - which has the name 'email' - we would use the form variable (created above), as such:
        //theForm.email 
        //you this with the name of any field iside of the form
        //alert(theForm.email.value);
    
        //use an if statement to check the value of the form
        var mailingVal = theForm.mailing.value,
           re = /@gmail\.com$/i;
        mailingVal = trim(mailingVal);
    
        if(!re.test(mailingVal)){
           //error message
    
           //add a dropshadow to the field (to highlight it)
           theForm.mailing.style.boxShadow = "0px 0px 6px #01FFFF";
    
           //from the form field, go up to the parent (the div with the class 'formbox', then inside of that for the div with the class 'fieldInfo', and change the text contents to be an error message
           setMessage(theForm.mailing, "error", "You must enter an address");
           /*theForm.email.parentNode.querySelector("div").innerHTML = "You must enter an email!";
           theForm.email.parentNode.querySelector("div").className = "error";*/
    
        } else {
    
          //if the user entered an email (or in this anything) give them positive feedback
          theForm.mailing.style.boxShadow = "";
    
          setMessage(theForm.mailing, "correct", "Perfect");
    
          /*theForm.email.parentNode.querySelector("div").innerHTML = "Perfect)"
          theForm.email.parentNode.querySelector("div").className = "correct";*/
      }   
    }
    

    这应该适合你。关于您正在使用的trim() 函数,我确实有一个问题。它是什么?是否有您正在使用的库,或者您是否编写了修剪功能?我只会使用String.prototype.trim 删除mailingVal 开头和结尾的空格。

    【讨论】:

    • 我不确定我在这里做错了什么,我删除了 if(mailingVal == "" ) 并用你的 if 语句替换它并添加了第二个变量,它仍然接受任何即使没有 @anything 的字符
    【解决方案3】:

    如果您确切地知道要检查的邮件供应商,那么试试这个:

    if (mailingVal.length && mailingVal.indexOf('@gmail.com') > -1 ) console.log('that is gmail!'); 
    

    您可能还需要将您的字符串放入情人案例中,以确保“Gmail”也是有效的

    mailingVal = mailingVal.toLowerCase()
    

    更新: 正如 cmets 中提到的那样,这种情况将使像“wut@gmail.commadot”这样的邮件也有效。 为防止这种情况发生,您可以尝试以下检查:

    mailingVal = mailingVal.split['@'];
     if (mailingVal.length > 2) {
       console.log('not Valid email');
     } else {
       if (mailingVal[1].toLowerCase() === 'gmail.com') console.log('Bingo!');
     }
    

    【讨论】:

    • var mailingVal = 'wut@gmail.commadot';
    • 谢谢,但它仍然接受表单框中的任何字符,而不仅仅是@gmail.com
    • var mailingVal = 'wut\@wut"@gmail.com';
    猜你喜欢
    • 2018-03-13
    • 2017-07-12
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多