Question

Here is my current function:

function isEmail(str){
 var emailReg = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$';
 var regex = new RegExp(emailReg);
 return regex.test(str);
} 

if(ecEmail != '' && ecEmail != null){
 if(isEmail(ecEmail) == false){
  alert('Please provide a valid email address.');
  return false;
 }
}

My validation is not allowing for an email containing the "&" sign such as hunter&staff@gmail.com. I need help with a new email-validation in order to allow for an email with the "&" sign.

Was it helpful?

Solution

You should be able to use:

'^[\\w\-&_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$'

or

'^[\\w\-\&_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$'

or

'^[\\w\-&_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$'

OTHER TIPS

Try this regex:

var patt=/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|<94>(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*<94>)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;

I found that off w3.org a while ago, unfortunately, can't find the direct link at the moment.

EDIT:

w/ the proper var name:

var emailReg =/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|<94>(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*<94>)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top