[英]JavaScript Validation for top level domain of email address
I am trying to create some JavaScript validation to only allow the input of a top level domain of an email address. 我正在尝试创建一些JavaScript验证,以仅允许输入电子邮件地址的顶级域。 eg(@gmail.com)
例如(@ gmail.com)
I have used the snippet below to try and form validation around the input, however it still allows full email addresses to be input. 我已使用下面的代码段尝试对输入内容进行表单验证,但是仍然可以输入完整的电子邮件地址。 eg (mark@gmail.com)
例如(mark@gmail.com)
function isEmailDomain(emailDomainVar) {
var regEmail = new RegExp('@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/');
return regEmail.test(emailDomainVar);
}
Thanks in advance for any input on this issue! 预先感谢您对此问题的任何投入!
There is a symbol for matching the beginning of the imput: ^
. 有一个符号与输入的开头匹配:
^
。 Simply add it in the beginning of your regex: 只需将其添加到正则表达式的开头即可:
function isEmailDomain(emailDomainVar){
var regEmail = /^@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
return regEmail.test(emailDomainVar);
}
Here is MDN docs ref for further reading. 这是MDN文档参考,供您进一步阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.