简体   繁体   中英

Email Address Validation Regex

So I want to set up an Email Address Validation Regex with the following constraints...
reference: α @ β . γ

for α
1) Only contain az 0-9 . - _
2) Contain between 1 and 30 chars (inclusive)
3) Can't start or end with . - _
4) Can't have 2 consecutive symbols (out of . - _ ) in it

for β
1) Only contain az 0-9 -
2) Contain between 2 and 14 chars (inclusive)
3) Can't start or end with -
4) Can't have 2 consecutive - in it

for γ
1) Only contain az
2) Contain between 2 and 4 chars (inclusive)

===========================================================================

The first two constraints of each are easy to implement, So I have done it, as follows
([a-z0-9.\-_]{1,30})@([a-z0-9\-]{2,14}).([az]{2,4})

Can anyone help me with (2) and (3) of α and β

You may use

/^(?=[^@]{1,30}@)[a-z0-9]+(?:[._-][a-z0-9]+)*@(?=.{2,14}\.[a-z]+$)[a-z0-9]+(?:-[a-z0-9]+)*\.[a-z]{2,4}$/

See the regex demo

Details

  • ^ - start of string
  • (?=[^@]{1,30}@) - 1 to 30 chars are allowed before a @
  • [a-z0-9]+ - 1+ lowercase letters and digits
  • (?:[._-][a-z0-9]+)* - 0 or more occurrences of . , _ or - followed with 1+ lowercase letters or digits
  • @ - a @ char
  • (?=.{2,14}\.[az]+$) - immediately to the right, there must be 2 to 14 chars, then . and 1+ lowercase letters till the end of string
  • [a-z0-9]+(?:-[a-z0-9]+)* - 1+ lowercase letters and digits and then 0 or more occurrences of a hyphen followed with 1+ lowercase letters or digits
  • \. - a dot
  • [az]{2,4} - two to four lowercase letters
  • $ - end of string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM