Search code examples
regexvalidationemail-validation

Email regex validation that does not allow email's to end in .mil


I am trying to find/write a regex that does not allow the email address to end in .mil when input into a form.

This is what I have to allow ONLY emails ending in .mil, but in this instance I would like the opposite. My understanding of RegEx is amateur at best:

^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.mil)$

Thanks! If you're willing, a break down of the different pieces or the change would be very helpful so I understand better moving forward.


Solution

  • Use a negative lookbehind at the end.

    ^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(?<!\.mil)$