电子邮件地址验证javascript for multiple' +'迹象

时间:2014-07-18 09:16:36

标签: javascript validation email

我正在尝试验证用户在javascript中输入的电子邮件地址

var email = document.getElementById("<%=Email.ClientID%>").value;
                // checks if the e-mail address is valid
                var emailPat = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;
                var matchArray = email.match(emailPat);
                if (matchArray == null) {
                    alert("Not a valid e-mail address");
                    document.getElementById("<%=Email.ClientID%>").focus();

                    return false;


                }

这很有用,可以验证电子邮件地址,但在此电子邮件地址返回false ayaz5+any+thing+you+want@gmail.com我想要的是这也是一个有效的电子邮件地址,我需要在此感谢中做出哪些更改

1 个答案:

答案 0 :(得分:1)

试试这段代码:

var email = 'ayaz5+any+thing+you+want@gmail.com';
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var matchArray = regex.test(email);
if (matchArray) {
    alert("valid");
} else {
    alert("Not a valid e-mail address");
}

DEMO