I have this regex [a-z0-9]*@metu\.edu
which checks user input userside, using HTML5. I want to accept a dot (.) within the username, only a single dot. Such as: herp.derp@metu.edu

- 8,700
- 35
- 117
- 201
-
You might find [this](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) post on SO helpful. – FrVaBe Aug 25 '11 at 09:07
5 Answers
-
-
You dropped the digits... ilhan wrote: "only alphanumeric with a single dot". – Lucero Aug 25 '11 at 09:21
-
This will match `herp.@metu.edu` If that's undesirable, use `^[a-z0-9]+(\.[a-z0-9]+)?` - Matches `herp` & `herp.derp` & `herpderp` but not `.herp` or `herp.` – user Jun 08 '14 at 17:45
[a-z0-9][a-z0-9.]*@metu\.edu\.tr
This will require at least one (lower case character or number) and then (lower case character or number or dot), so email addresses cannot begin with or only consist of a single dot. However you should probably reconsider using this regex for email validations, as the acceptable email addresses contain a lot more cases (-, +, _ etc.) (and don't forget size limitations as well)

- 1,101
- 9
- 7
-
You're right, plus it also allows dot at the end of the local part, which is not formally valid according to RFC5322. – Zaki Aug 25 '11 at 09:22
-
Add a dot to the character range: [a-z0-9.]*
To exclude dots at the beginning or end of the name, and only allow a single dot use multiple character classes:
[a-z0-9]+\.?[a-z0-9]+@metu\.edu

- 28,903
- 6
- 107
- 121
-
This is a single character optionally followed by a dot, you are probably missing a "+" after the first character class. – Zaki Aug 25 '11 at 09:12
-
@Zaki you are right, I added a `+`, and changed the last `*` as well. Tested and is matching `herp.derp@metu.edu` now but not `herpderp.@metu.edu` or `herp.derpity.derp@metu.edu` – kapex Aug 25 '11 at 09:13
In general, e-mail addresses shouldn't be matched with regexes. However, in your specific case, it seems that you have a distinct pattern that you want to match against.
[a-z0-9]+(\.[a-z0-9]+)?@metu\.edu
Assuming that the single dot is optional, if it's mandatory, use this:
[a-z0-9]+\.[a-z0-9]+@metu\.edu

- 59,176
- 9
- 122
- 152
You should not be trying to parse e-mail adresses yourself, using regex, as you WILL fail. Please consider this: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
What I usually do is accept any string as e-mail. If it's just for user registration or whatever, there is really no need to validate it. It will fail when you send out an email, then you know it's wrong.

- 5,817
- 3
- 27
- 23
-
This is for a university. We don't have different kinds of expressions. only alphanumeric with a single dot. – ilhan Aug 25 '11 at 09:10