Sunday, March 24, 2019

javascript - regex to validate emails which allows only these two special symbols .,- before @ symbol




I have used below regex for email validation.



var reg = /^(([^<>()[\]\\.,;:\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,}))$/;


How can i validate the below two email id like





  1. hello!%#%^world@gmail.com

  2. helloworld@-gmail.com



Vaild Email ids:




  1. hello-world@gmail.com

  2. hello.world@gmail.com




InVaild Email ids:




  1. hello!#%#@gmail.com

  2. helloworld@-gmail.com



Special characters should not be allow before @gmail.com




Below is my JavaScript code



var re = /^(([^<>()[\]\\.,;:\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,}))$/;

if (!re.test($(this).val())) {
errmsg.push($(this).attr('placeholder') + ' is invalid');
}

Answer




You could try the below regex to validate emails which allows only these two special symbols .,- before @ symbol.



^[^<>()[\]\\,;:\%#^\s@\"$&!@]+@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}))$


DEMO


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...