使用javascript验证表单中的特定电子邮件地址
我正在设置一个表单,在表单中我已经编写了代码,以验证电子邮件表单框中是否有条目,如您所见使用javascript验证表单中的特定电子邮件地址,javascript,html,css,validation,email,Javascript,Html,Css,Validation,Email,我正在设置一个表单,在表单中我已经编写了代码,以验证电子邮件表单框中是否有条目,如您所见 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 fi
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。任何指导都将不胜感激,谢谢 如果您确切知道要检查哪个邮件供应商,请尝试以下方法:
if (mailingVal.length && mailingVal.indexOf('@gmail.com') > -1 ) console.log('that is gmail!');
你可能还需要把你的字符串放在情人箱里,以确保“Gmail”也是有效的
mailingVal = mailingVal.toLowerCase()
UPD:
如评论中所述,此案例将产生类似以下邮件: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!');
}
您可以使用正则表达式:
if (mailingVal && mailingVal.match(/@gmail\.com$/i)) {
// it's gmail
}
更好的方法可能是使用正则表达式,确保要匹配的字符串以
@gmail.com
var re = /@gmail\.com$/i;
if(re.exec(mailingVal) !== null) {
// the test passed!
}
这将确保字符串以@gmail.com
结尾,并且在.com
之后不包含任何额外字符
var re = /@gmail\.com$/i;
if(re.exec(mailingVal) !== null) {
// the test passed!
}
使用该正则表达式,someone@gmail.com
将匹配,但someone@gmail.comm
将不会。正如someone@Gmail.com
或someone@GMAIL.COM
(依此类推),因为开关为/i
如果您想让它只区分大小写,只需删除/i
开关,这样正则表达式就会像
var re = /@gmail.com$/
更新
这是代码中的正则表达式解决方案,将exec
更改为test
(根据正则表达式是否匹配,它只返回true或false):
这应该对你有用。关于您正在使用的
trim()
函数,我确实有一个问题。这是怎么一回事?您是否正在使用库,或者trim函数是您编写的?我只需要使用String.prototype.trim
删除mailingVal
开头和结尾的空白 var mailingVal='1〕wut@gmail.commadot';代码>谢谢,但它仍然接受在表单框中写入的任何字符,而不仅仅是@gmail.comvar mailingVal='wut\@wut'@gmail.com';
我认为解决方案应该是电子邮件:@gmail.com
。(同样来自用户体验)。那么您就不需要在此进行任何验证。我不确定我在这里做错了什么,我删除了if(mailingVal==“”),并将其替换为您的if语句,添加了第二个变量,即使没有@anything,它仍然接受任何字符