-3

I have to modify a script which uses regex to check whether given email is valid or not. Its working fine but failed to validate email like "foo$!@bar.com".

Here is the regex :- var tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/; Can anyone help me in this. Thanks in advance.

Manish Saraan
  • 167
  • 2
  • 14
  • search a little more, you will find answer.. You can use `validator.js` an _npm_ package to validate or you can use `regex101` (google it) to verify your regex against test cases – Muhammad Faizan Apr 06 '18 at 12:03
  • As per W3C guidelines $ is vaild in personal part. https://www.w3resource.com/javascript/form/email-validation.php – jeshu911 Apr 06 '18 at 12:03
  • Why are you trying to validate an email address accepting characters that are not valid to an email address? Have you tried escaping special characters? – muecas Apr 06 '18 at 12:04
  • Actually its a function in my code. I think i need to replace it all with a library. – Manish Saraan Apr 06 '18 at 12:08
  • Possible duplicate of [How to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – Björn Apr 06 '18 at 12:14
  • 1
    @Bjorn I don't think those examples will match emails containing special characters – DNKROZ Apr 06 '18 at 12:16
  • @DNKROZ you are right. sorry. should have looked better. – Björn Apr 06 '18 at 13:40

1 Answers1

0

I think this is what you're looking for:

^[a-zA-Z0-9.!#$%&'*+=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$

https://regex101.com/r/gvucy3/1

DNKROZ
  • 2,634
  • 4
  • 25
  • 43