Kautil
Kautil

Reputation: 1331

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.

Upvotes: 0

Views: 370

Answers (3)

Jayraj Goswami
Jayraj Goswami

Reputation: 1

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

Upvotes: 0

Mulan
Mulan

Reputation: 135227

This should work

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

Regular expression visualization

It will validate

  • empty strings, OR
  • strings that have one or more non-@
  • followed by a @
  • followed by one or more non-@

enter image description here

Upvotes: 1

Vinod Louis
Vinod Louis

Reputation: 4876

try this here is the fiddle

 var emailReg = /^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/;

Upvotes: 0

Related Questions