简体   繁体   English

电子邮件地址的jQuery正则表达式验证,包括空白值

[英]jQuery regex validation of e-mail address including blank value

I am using custom[email] rule for email validation in Jquery Validation Engine. 我正在使用custom [email]规则在Jquery Validation Engine中进行电子邮件验证。 What I want is, I want regex that validates email with blank value also. 我想要的是,我也想使用正则表达式来验证具有空值的电子邮件。 If value is blank then also it should show error message. 如果值是空白,那么它也应该显示错误消息。 I dont want to use required rule. 我不想使用必填规则。

Here is the custom rule given in Jquery Validation Engine 这是Jquery Validation Engine中给出的自定义规则

"email": {
         // HTML5 compatible email regex ( http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#    e-mail-state-%28type=email%29 )
         "regex": /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
         "alertText": "* Invalid email address"
}

Please help me out. 请帮帮我。

This should work 这应该工作

^([^@]+@[^@]+)?$

正则表达式可视化

It will validate 它将验证

  • empty strings, OR 空字符串,或
  • strings that have one or more non- @ 具有一个或多个非@字符串
  • followed by a @ 跟一个@
  • followed by one or more non- @ 随后是一个或多个非@

在此处输入图片说明

试试这是小提琴

 var emailReg = /^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/;
 function checkEmail(email) {
            var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
            var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
            if (!reg1.test(email) && reg2.test(email)) {
                return true;
            }
            else {
                return false;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM