0

I'm using this regex /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/ for validating email format but when I leave the the email field blank it shows an error that the format is invalid. Can somebody help me how to add a regex for empty string?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
james
  • 287
  • 3
  • 5
  • 17
  • 1
    Just check whether the email field is empty before applying the regex? – poke Oct 14 '15 at 06:57
  • 1
    Note that your regex is very bad for emails, and [you shouldn’t validate email addresses like that anyway](http://davidcel.is/posts/stop-validating-email-addresses-with-regex/). – poke Oct 14 '15 at 06:58
  • @poke Checking for an empty field may not be possible in all cases. I'm in that situation right now with cakePHP framework. – Austin Poulson Nov 02 '22 at 16:05

3 Answers3

1

You can use the OR | operator with ^$ to match the empty string.

/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}$|^$/

0
^(?:[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6})?$

You can just put all into a group and make the group optional.See demo.

https://regex101.com/r/mG8kZ9/12

vks
  • 67,027
  • 10
  • 91
  • 124
0

I was in same need few days back, so i created a separate function for it ,so it can be used by multiple email fields , and other text fields as well.

Although the Regex provided is for all valid emails, you can restrict as per your own need.

 function IsValidEmailAddress(selectorElement) {

var emailAddress = $(selectorElement).val();
emailAddress = emailAddress.trim();

//All valid email pattern,a-z,0-9 etc [from stack overflow]
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);

//Test the pattern with input emailAddress
if (pattern.test(emailAddress)) {

   //Put your own logic here

    if ($(selectorElement).parent().hasClass('error')) { 
        $(selectorElement).parent().removeClass('error');
    }
    return true;
}

if (!IsNullOrEmptyElement(selectorElement)) {

  //put your own logic here

    $(selectorElement).parent().addClass('error');
    $(selectorElement).val('');
    $(selectorElement).attr("placeholder", "Add Valid Email Address.");
}
return false;
};


//Function checks wether the element is Null or empty
function IsNullOrEmptyElement(selectorElement) {
var textboxText = $(selectorElement).val();

textboxText = textboxText.trim();

if (textboxText == null || textboxText == "") {
    $(selectorElement).parent().addClass('error');
    $(selectorElement).attr("placeholder", "This field is required.");
    return true;
}
$(selectorElement).parent().removeClass('error');
return false;

}

Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46