1

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

for α
1) Only contain a-z 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 a-z 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 a-z
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}).([a-z]{2,4})

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

GaurangiS
  • 85
  • 1
  • 6
  • Do you specifically want only those be valid that meet those criteria? Or is this the best approximation you came up with? – Fildor Jun 18 '20 at 08:40
  • Does this answer your question? [How to validate an email address using a regular expression?](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression) – Yevhen Horbunkov Jun 18 '20 at 08:41
  • 1
    Just test for an `@` and a dot `.` after the `@`. Anything else is useless. To verify the mail address send a valdiation mail to that address. – Andreas Jun 18 '20 at 08:41
  • Try `^(?=[^@]{1,30}@)[a-z0-9]+(?:[._-][a-z0-9]+)*@(?=.{2,14}\.[a-z]+$)[a-z0-9]+(?:-[a-z0-9-])*\.[a-z]{2,4}$`. However, it does not allow `.-` or `.-` or `-._` in the username part which is not mentioned in the requirements. – Wiktor Stribiżew Jun 18 '20 at 08:42
  • `^(?=[^@]{1,30}@)[a-z0-9]+(?:[._-][a-z0-9]+)*@(?=.{2,14}\.[a-z]+$)[a-z0-9]+(?:-[a-z0-9-])*\.[a-z]{2,4}$` works for `α` but it breaks when `-` is added to `β` – GaurangiS Jun 18 '20 at 08:54
  • The best way to validate an email address is to send an email and check the return value. Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Jun 18 '20 at 09:17
  • @GaurangiS I think there is a typo, use `^(?=[^@\n]{1,30}@)[a-z0-9]+(?:[._-][a-z0-9]+)*@(?=.{2,14}\.[a-z]+$)[a-z0-9]+(?:-[a-z0-9])*\.[a-z]{2,4}$` if you test at a regex tester. See a [regex fiddle](https://regex101.com/r/nAUcIv/1) – Wiktor Stribiżew Jun 18 '20 at 09:42

1 Answers1

0

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}\.[a-z]+$) - 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
  • [a-z]{2,4} - two to four lowercase letters
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563