-1

I am attempting to create a regular expression for validating edu emails which may or may not have a sub domain. Some schools have emails like "example@hms.harvard.edu" while other schools have emails like "example@stanford.edu".

([0-9]|[a-z]|[A-Z])+@([0-9]|[a-z]|[A-Z])+([0-9]|[a-z]|[A-Z])\.edu$"

This is the current regular expression that I have but I am not well versed in these. I am looking to create an expression that will validate emails with one domain and emails with a subdomain.

Any help would be appreciated. Thanks!

gt123
  • 1
  • 1

2 Answers2

0

In most cases, /^[-\w.]+@[-\w.]+\.edu$/ should be enough. The only problem with this solution would be, that it can also accept domains like hms..harvard.edu.

To prevent this, you could use this regex instead: /^[-\w.]+@([-\w]+\.)*[-\w]+\.edu$/

Edit: use \w instead of [0-9a-zA-Z_]

Kurt Thiemann
  • 324
  • 1
  • 6
  • Thank you for the response. Would the +\. in the ([-\w]+\.) create an extra period in the case of one domain? – gt123 Aug 21 '20 at 00:03
  • ([-\w]+\.) matches one or more alphanumeric characters and dashes followed by a period (in your example this would be the 'hms.' part). The '*' quantifier defines that this group can occur 0 or more times (so there must not be a subdomain, but there can be multiple subdomains). Therefore 'example@harvard.edu' and 'example@hms.something.harvard.edu' would both also be considered a valid email address. – Kurt Thiemann Aug 21 '20 at 01:52
0

This should do the trick (modified from http://emailregex.com/ - referenced in How to validate an email address using a regular expression?):

/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+edu))$/
PotatoParser
  • 1,008
  • 7
  • 19