Jason
Jason

Reputation: 15378

how to validate an email address to this form: [email protected]

I'd like to know a regex that would validate: an email address to this form: [email protected]

couple issues:

"student." is optional and could be any word eg "teacher.".

"324234234" can be any alpha numeric characters (number, word, _ etc.)

the email must end in "uws.edu.au"

This is what I have so far:

/(\d*)@\w*\.uws\.edu\.au/

valid addresses:

[email protected] [email protected] [email protected]

etc.

Thanks Guys

Upvotes: 0

Views: 173

Answers (2)

Lee
Lee

Reputation: 13542

You said:

Just tried /(\w*)@(\w*.)?uws.edu.au/ and that seemed to work. Any further suggestions are welcome – Jason 4 secs ago

Your regex will match "@teacher.uws.edu.au" (i.e. "name portion" omitted).

To fix this, you could use:

/(\w+)@(\w+\.)?uws\.edu\.au/

Which will require at least one character in the name portion, and at least one char before the dot (if there is a dot) in the subdomain spot.

Also (I think) that \w will not match . (and probably other chars that you care about in the name portion too), so [email protected] would fail to match. The following would add the char ., _, and - into the "name" portion:

/([\w\._-]+)@(\w*\.)?uws\.edu\.au/

you could add any other chars you need in the same way.

NOTE: Matching email addresses in general a more complex thing than you might think (lots of strange things are technically allowed in email addresses. Here is an article on the subject (There are many other sources of similar information available).

Upvotes: 2

Greg
Greg

Reputation: 23503

Three thoughts:

  • Change the initial \d to \w to match "word" characters [a-zA-Z0-9_] instead of just digits.
  • Make the subdomain optional using ?
  • Use + instead of * when matching the username and subdomain. Otherwise @.uws.edu.au will validate.

Suggested:

/\w+@(\w+\.)?uws\.edu\.au/

Upvotes: 4

Related Questions