HTML 5 – Email Address Validation Regex

And interesting one to stumble upon – the ‘official‘ regex for validating a email address:

Edit: I’ve had to break this down into parts – for some reason WordPress throws a 403 error if I don’t. I think it sees this regex as a security risk!

Before the @ sign:
^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+

The @ sign and after:
@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$

Reads like it should work pretty well to me. I didn’t realise before the @ was so generous. And you could shrink it by using a case insensitive operator.

Advertisements
HTML 5 – Email Address Validation Regex

2 thoughts on “HTML 5 – Email Address Validation Regex

  1. Note that as specified in the spec you link to this regex is too strict regarding the part before the @ and too loose regarding the part after.

    A completely valid address like “name quot ” and at @ chars”@comp.com is going to be rejected

    Like

  2. I actually thought this was a bit loose on the bit before the @ – I was surprised that so many characters were allowed. And on the after part, yes, I thought it was loose as well – something like @example would be valid. I thought you had to have a TLD.

    Whichever, in 15 years of looking, I’ve never found a really satisfactory email address regex, so I’m almost willing to adopt a ‘standard’ one in the hope that eventually everyone complies…

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s