Home > Blockchain >  Email Validation refined
Email Validation refined

Time:12-28

I have following regex for email validation

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

only issue with this or similar solution available is that [email protected] passes this regex.

I do not want any email to start with a digit.

CodePudding user response:

^[a-zA-Z0-9.!#$%&' /=?^_`{|}~-] @

This is the part of the regex that matches everything up to the @ separator. 0-9. matches any digit. So create a new phrase before that that doesn't match 0-9. Then change the quantifier (currently , at least 1) to * (0 or more) so that emails with only one character work (as long as it's not a number of course).

^[a-zA-Z.!#$%&' /=?^_`{|}~-][a-zA-Z0-9.!#$%&' /=?^_`{|}~-]*@

See https://regex101.com/r/qjdZ0A/1 for an interactive example.

CodePudding user response:

Prepend the expression of the email with a negative lookahead...

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

This only matches the email if the first character is NOT 0-9

  • Related