1
votes

Need to validate email address with single domain level or multi level domains.(actually domain level <=4)

Criteria:

  • Email (username) should start with letter[a-z] and it may have numbers but not for 1st letter and periods(.) No other characters.
  • After @ sign every domain should start with letter.(minimum length 2)
  • Maximum domains <= 4 .

Ex: [email protected]

above example there 4 domains;

  • .my
  • .sub
  • .mail
  • .com

I try with this RegEx:

^[a-zA-Z](:?[a-zA-Z0-9._-])+(@[a-zA-Z]+[a-zA-Z0-9_-])+\.+(([a-zA-Z]){2,6})$

But above regex not validating multiple domains correctly.It's only get 1 domain. Ex: [email protected]

Online Regex : https://regex101.com/r/7SXS1Z/1

2
filter_var() with FILTER_VALIDATE_EMAIL???AbraCadaver
I've done this one filter_var($s_email, FILTER_SANITIZE_EMAIL);Elshan
@AbraCadaver I need to validate something like this-> [email protected] so if I used FILTER_VALIDATE_EMAIL itself it's says above email also valid technically.Elshan
Because that is a valid email, so you only want to accept certain valid emails...AbraCadaver

2 Answers

1
votes

Your regex applies much more rules in accepting an email address. E.g. allowing email addresses to include more than one @ symbol. Go as simple as your own rules:

^[a-z][^@]*@(([a-z][a-z0-9-]+)\.){0,3}(?2)$

Live demo

1
votes

Maybe this is what you're looking for

^
[a-zA-Z][-.\w]*       # before @
@
[a-zA-Z][-.\w]+       # first subdomain
(?:
    \.[a-zA-Z][-.\w]+ # eventually others
){1,3}
$

See a demo on regex101.com.