-1

I am writing a regular expression for checking valid email addresses.

"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.com"

How do I alter this to allow .edu and others?

jscs
  • 63,694
  • 13
  • 151
  • 195
Chris
  • 68
  • 1
  • 6

1 Answers1

0

Use | for Boolean "OR". You need to wrap the options in brackets, e.g. "Ben(jamin|edict)" means "Benjamin" or "Benedict", whereas "Benjamin|edict" means "Benjamin" or "edict".

"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+.(com|org|edu|net|gov|mil)"

Of course, this is failing to allow for country code top-level domains. It may be best to check if an email validation library exists for whatever programming language or framework you are using. E.g. Python has the validate_email module, PHP has FILTER_VALIDATE_EMAIL.

David Scarlett
  • 3,171
  • 2
  • 12
  • 28