简体   繁体   中英

jQuery regex validation of e-mail address including blank value

I am using custom[email] rule for email validation in 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

"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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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