0

Below regular expression for email validation,

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

Here I couldn't understand the [\w\.-] purpose. Can any one please let me know?

Use this pattern,

If I give [email protected] in a email field, validation becomes fail. In email string before @ if just one character or numeric comes the validation become fail. Is this right?

Regards, karthik

5
  • 1
    This is for Javascript? BTW, the regex looks wrong for its purpose. Where did you get it from? This would be a proper regular expression for email address validation. Commented Jul 23, 2012 at 9:50
  • 3
    why people want to use regex for e-mail validation? there are enough libraries for this task Commented Jul 23, 2012 at 9:58
  • @loldop: Can you please tell me libraries other than regex for email validation? Commented Jul 23, 2012 at 10:22
  • 1
    @Karthik yeah, i can: metacpan.org/module/Email::Valid. This is perl solution. Commented Jul 23, 2012 at 11:06
  • hey check out my answer. see if it works for you Commented Jul 23, 2012 at 11:12

2 Answers 2

2

[\w.-] means a single character that is either:

  • A word character (alphanumeric or _)
  • .
  • -

[email protected] fails because this regular expression requires at least 2 characters before the @.

Sign up to request clarification or add additional context in comments.

3 Comments

The trivial fix would be to change [\w\.-]*[a-zA-Z0-9] into ([\w.-]*[a-zA-Z0-9])? i.e. make the part after the first character optional (and remove the superfluous backslash in the character class).
@tripleee I can't tell from the original question whether rejecting [email protected] is desirable or not :P.
Ah, maybe the question is really "should I fix this"? Yes, you should.
-1

Use this regular expression for validating email id. Works fine for me

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 

Mark as answer if that worked for you :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.