Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active November 9, 2023 04:21
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhowellsmead/48f4f62acd7f75afaceaffab430c0345 to your computer and use it in GitHub Desktop.
Save markhowellsmead/48f4f62acd7f75afaceaffab430c0345 to your computer and use it in GitHub Desktop.
Regex for valid email address excluding specific free domains, like Gmail, GMX and Yahoo. Extend the list of domains according to your own requirements.
^([\w.-]+)@(\[(\d{1,3}\.){3}|(?!hotmail|gmail|googlemail|yahoo|gmx|ymail|outlook|bluewin|protonmail|t\-online|web\.|online\.|aol\.|live\.)(([a-zA-Z\d-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$
@nitheesh
Copy link

nitheesh commented May 26, 2021

Thanks for this snippet. we need to update the TLD length on the 6th capturing group to at least 10-15 charectors to match the TLD like .agency, .productions, .photography, etc

^([\w.-]+)@(\[(\d{1,3}\.){3}|(?!hotmail|gmail|googlemail|yahoo|gmx|ymail|outlook|bluewin|protonmail|t\-online|web\.|online\.|aol\.|live\.)(([a-zA-Z\d-]+\.)+))([a-zA-Z]{2,15}|\d{1,3})(\]?)$

@markhowellsmead
Copy link
Author

Thanks @nitheesh! According to https://www.icann.org/en/system/files/files/ua-factsheet-a4-17dec15-en.pdf, the maximum length of a TLD is 63 characters. The following snippet will cover that.

^([\w.-]+)@(\[(\d{1,3}\.){3}|(?!hotmail|gmail|googlemail|yahoo|gmx|ymail|outlook|bluewin|protonmail|t\-online|web\.|online\.|aol\.|live\.)(([a-zA-Z\d-]+\.)+))([a-zA-Z]{2,63}|\d{1,3})(\]?)$

@alexk1350
Copy link

Thank you for sharing, do you know how to make it not case sensitive? For example if I type xxxx@Gmail.com validation doesn't work.

@markhowellsmead
Copy link
Author

@alexk1350 A regular modifier for case-insensitivity should be sufficient.

@alexk1350
Copy link

Got it, honestly, I'm not an expert in Regex, snipped you have released is almost perfect, worked well with our CMS. Will start to torture my backend devs:) have no idea what is "A regular modifier for case-insensitivity ". Thank you for making it public!

@markhowellsmead
Copy link
Author

It depends which technology you're using. E.g. for PHP look at flags in preg_match.

@alexk1350
Copy link

.net What you have hear "^([\w.-]+)@([(\d{1,3}.){3}|(?!hotmail|gmail|googlemail|yahoo|gmx|ymail|outlook|bluewin|protonmail|t-online|web.|online.|aol.|live.)(([a-zA-Z\d-]+.)+))([a-zA-Z]{2,4}|\d{1,3})(]?)$" works well with exception of case sensitivity. gMail, oUtlook etc. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment