电子邮件验证JavaScript [英] email validation javascript
问题描述
函数checkTextBox(textBox)
{
if(!checkValidity(textBox.getValue()))
displayError(Error title,Error message,textBox);
textBox.focus();
}
函数checkValidity(e)
{
var email;
email =/^[^@]+@[^@]+.[a-z]{2,}$/i;
if(!e.match(email)){
return false;
else
返回true;
}
}
编辑:回答赞赏!感谢!
/rfc5322#section-3.4rel =noreferrer> RFC 5322,§3.4 。相关的非终端是 addr-spec
。由于领域规范的复杂性和支持陈旧,过时的形式,这个定义变得有些诡异。但是,您可以对大多数表单进行过度逼近: ^ [ - 0-9A-Za-z!# $%&放大器; '* + / = ^ _`{|}〜] + @ - 0-9A-ZA-Z#$%&放大器;?!' * + / = ^ _`{|}〜? ] +
请注意,有大量的合法字符。大多数reg-exs都会列出错误。是的,所有这些字符在电子邮件地址中都是合法的。
这个正则表达式不匹配一些非常用的形式,比如noodle soup @ 9@ [what.example.com]
- 这是一个合法的电子邮件地址!
is this javascript function (checkValidity) correct?
function checkTextBox(textBox)
{
if (!checkValidity(textBox.getValue()))
displayError("Error title", "Error message", textBox);
textBox.focus();
}
function checkValidity(e)
{
var email;
email = "/^[^@]+@[^@]+.[a-z]{2,}$/i";
if (!e.match(email)){
return false;
else
return true;
}
}
EDIT: All the answers appreciated! Thanks!
E-mail address are defined in RFC 5322, § 3.4. The relevant non-terminal is addr-spec
. The definition turns out to be somewhat squirelly, due to both the complications of domain specifications and supporting old, obsolete forms. However, you can do an over-approximation for most forms with:
^[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+@[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+
Notice that there are a very large number of legal characters. Most reg-exs get the list wrong. Yes, all those characters are legal in an e-mail address.
This regex will not match some very uncommon used forms like "noodle soup @ 9"@[what the.example.com]
-- which is a legal e-mail address!
这篇关于电子邮件验证JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!