matt
matt

Reputation: 44303

Simple email-regexp doesn't allow hyphen before and after @

I found this simple regexp (i know it's probably not perfect) somewhere online to validate an email address.

/^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/

The problem is, that this regexp doesn't allow for the following case:

[email protected]
[email protected]

Any ideas?

ps. I'm using this regexp within javascript.

Upvotes: 2

Views: 2767

Answers (1)

speakr
speakr

Reputation: 4209

If you simply want to add hyphens you can change the regexp to:

/^(?:\w+[\-\.])*\w+@(?:\w+[\-\.])*\w+\.\w+$/

To add other special chars e.g. like underscore just put them in the first (not the second) pair of square brackets, i.e. change [\-\.] to [\-\._].

Also have a look on this question and its anwer.

Upvotes: 5

Related Questions