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:
- 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.