0

i am using the regex below to validate email

const regex = /[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;

i am trying to validate all the emails in the wiki here

https://en.wikipedia.org/wiki/Email_address#Local-part

the above regex works for all except the below valid emails are invalid and invalid emails are tested valid.

valid strings

"john..doe"@example.org
" "@example.org
admin@mailserver1   

invalid strings

A@b@c@example.com
a"b(c)d,e:f;g<h>i[j\k]l@example.com
just"not"right@example.com

how can i fix the regex to work for these too. could someone help me fix this regex? thanks.

EDIT

what i have tried based on answer provided

const regex = (?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])).){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-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])+)]);

but gives me error expression expected at here (? starting of expression

someuser2491
  • 1,848
  • 5
  • 27
  • 63
  • from the link i have used this (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-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])+)\]) and gives me error expression expected. i have modified my question to what i have tried. – someuser2491 Aug 07 '20 at 11:42
  • It's not worth the trouble. Test for `@` and a dot after the last `@` - that's good enough. If you need a working mail address then send a verification mail with a link to activate the account/mail address. – Andreas Aug 07 '20 at 11:42
  • but i need to do it for all of the email address :/ – someuser2491 Aug 07 '20 at 11:47
  • 1
    This can be error prone with all the different characters that you accept and not accept, but perhaps you can fiddle around with matching either the allowed chars between double quotes only or without the double quotes and at the end of the pattern, make the part with the dot optional https://regex101.com/r/vRDvXe/1 – The fourth bird Aug 07 '20 at 11:49
  • There's no perfect regex for email addresses. Use whatever suits you, I usually go for a short one, as it's unlikely you'll encounter a string "extremely resembling an email address, but not really". – Robo Robok Aug 07 '20 at 11:54
  • @The fourth bird: your solution works perfect. i have a button that is left disabled if the email is invalid. so when i type say user@mailinator. then the button goes disabled when user starts to type another letter say user@mailinator.s then the button is enabled again. how can i handle that?thanks – someuser2491 Aug 07 '20 at 12:04
  • 1
    @someuser2491 Nice that it works. In that case, I would suggest creating a new question and adding the code that does not work for you. – The fourth bird Aug 07 '20 at 12:08

0 Answers0