Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex verification for different email cases

I want to use a regex code that does not allow two periods(.) in the middle, end with a period(.), or start with a period(.) for the "name"(name@test.com) of the email. For one of my negative case examples, I tested test.@test.com and it is accepting that email still. In my code I added (?!.*\.$) which should not be allowing the end of a period(.).

This is the regex code I am using:

^(?!\.)(?!.*\.$)(?!.*\.\.)[-a-zA-Z0-9._%+-]+\@(?!\.)(?!.*\.$)(?!.*\.\.)[-a-zA-Z0-9._%+-]+[^.]+\.(?=.*[a-zA-Z].*[a-zA-Z])[a-zA-Z0-9._%+-]+$

positive cases example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • test@test.com
  • test.test+test@test.tv

negative cases example:

  • .test@test.com
  • test..test@test.com
  • test.@test.com (this is the case it is allowing but not supposed to)

>Solution :

You can add a negative lookaround that checks when there’s no dot before the ampersand (?<!\.)\@:

^(?!\.)(?!.*\.$)(?!.*\.\.)[-a-zA-Z0-9._%+-]+(?<!\.)\@(?!\.)(?!.*\.$)(?!.*\.\.)[-a-zA-Z0-9._%+-]+[^.]+\.(?=.*[a-zA-Z].*[a-zA-Z])[a-zA-Z0-9._%+-]+$

Check the demo here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading