【发布时间】: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