coder101
coder101

Reputation: 1605

JS Regex for email allowing asterisk

I have a JS Regex which checks the validity of an email address. It all works good, however lately i noticed that it allows an asterisk * as a valid part of the email. I know some users maybe using that i don't want my application to allow that.

So far the code is :

  RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/

Kindly, advise how to change this so that * becomes an invalid character in the email provided by users.

Upvotes: 0

Views: 801

Answers (1)

ProgramFOX
ProgramFOX

Reputation: 6390

Just remove the escaped asterisks from your regex. Then your regex would become:

 RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/

[EDIT]

Don't remove all asterisks. Remove only the escaped asterisks.

Upvotes: 3

Related Questions