0

Possible Duplicate:
What is the best regular expression for validating email addresses?

I am using the following regular expression to validate e-mail addresses:

^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$

It doesn't consider the following e-mail address valid: username@q.com, but according to our customer it is a valid e-mail.

How do I modify the following regular expression statement to accept username@q.com as a valid e-mail address?

Community
  • 1
  • 1
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
  • 1
    The regex pattern you posted was specifically designed to catch common errors at the cost for disallowing some valid email addresses. It surely allows some invalid email addresses at too. – ikegami Jun 07 '11 at 17:45
  • Comments about using regex to solve this notwithstanding, this specific section @[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\. is what is not matching in this case because it is looking for at least 2 characters between "@" and "." There is a good short reference at [regular-expressions.info](http://www.regular-expressions.info/quickstart.html) and a number of sandbox tools listed in http://stackoverflow.com/questions/463796/is-there-a-good-browser-based-sandbox-to-practice-regex – cordsen Jun 07 '11 at 18:27

1 Answers1

1

Why do you need a regular expression to check the validity of an e-mailadres? If I would fill in fake@fake.com the regular expression would also say it is valid.

In my opinion, regular expressions take a lot of runtime to evaluate, and mostly do not make you accomplish your goal. Think about why you actually need it, and perhaps go for a simpler one that just checks if it is of the form #@#.# (#=some text) See: http://www.linuxjournal.com/article/9585

Vincent Koeman
  • 751
  • 3
  • 9