-1

I'm currently using below regex to check emails,

\w+@\w+\.\w+

but I would like to force the user to use their school domain (mark@college.edu), how do I accomplish this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rahm
  • 1
  • 1

1 Answers1

0

If you have a specific college name, let's suppose it as college then validation is pretty easy. Following regex does that.

Regex: [\w0-9\.-]*@college\.edu

Explanation:

  • [\w0-9\.-]* matches the first part of the email which might contain Latin alphanumeric characters, dot, underscore. - hyphen should be added either at beginning or end of character class.

  • @college\.edu checks for the specific college's domain name.

Regex101 Demo

  • thanks for the help. If the email address contains a hyphen (ex. m-example@college.edu) would this be correct ? [-\w0-9\.]*@college\.edu – Rahm Mar 05 '16 at 18:24
  • Yes ! absolutely. Just take precaution to add `-` at beginning or end of regex. Since you didn't mentioned `-` in your question I didn't added it. –  Mar 05 '16 at 18:29