Jump to content

Refining Email RegEx Validation


Psycho

Recommended Posts

I have an email validation expression that works, but I want to see if there is a way to refine one part and/or add one additional check.

 

Here is the expression:

 

/^[\w\+-]+([\.]?[\w\+-])*@[a-zA-Z0-9]{2,}([\.-]?[a-zA-Z0-9]{2,})*\.[a-zA-Z]{2,4}$/

 

The BOLD blue section ensures the username consists of 1 or more valid characters (alpha, numeric, '_', '+', '-')

The blue section allows the user to have zero or more sub elements (separated w/ a period) with the same valid characters

The BOLD red section ensures the domail consists of 2 or more valid characters (alps, numeric)

The red section allows the domain to have zero or more sub domains (separated w/ a period or '-') and the same valid characters [yes, the dash is not a sub-domain delimiter, but it is not acceptable at the begining or end of a domain just like the period]

The BOLD green section ensures the TLD consists of 2 to 4 acceptable characters (alpha or numeric)

 

So, my question are:

 

1) Can the blue sections be combined more efficiently into one such that the usrename can accept a period, but not at the beginning or end, and cannot appear in succession?

 

2) Can the red section be combined into one such that a period or dash cannot be at the beginning or end - and they cannot appear in succession of each other?

 

3) How can I add a minimum/maximum check to the entirety of the blue section and/or the (red + green sections)? I want to ensure the username is between 1-64 characters and the domain is between 5-255 characters.

 

Currently for #3 I am using a second regex test as follows:

 

/^(.{1,64})@(.{4,255})$/

Link to comment
Share on other sites

I think it's best the way you have it, with a few improvements:

 

Blue: [-\w+]+(?:\.[-\w+])*

Red: [a-z\d]{2,}(?:[-.][a-z\d]{2,})* (with the /i modifier)

 

I tried grouping everything into the following, but I received "Compilation failed: regular expression too large":

%

^

### User name

(?:

[\w+]

|

(?<!^|\.) \. (?![@.])

){1,64}

@

### Domain

(?:

[a-z\d](?=[a-z\d])

|

(?<=[a-z\d])[a-z\d]

|

(?<![-.]) [-.] (?![-.])

){4,255}

### Extension

\.[a-z]{2,4}

$

%ix

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.