1

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

6
  • Try ^[a-zA-Z][a-zA-Z0-9._-]+@(?:[a-zA-Z][a-zA-Z0-9_-]+\.){1,4}[a-zA-Z]{2,6}$ Commented Jun 26, 2017 at 17:58
  • 1
    filter_var() with FILTER_VALIDATE_EMAIL??? Commented Jun 26, 2017 at 18:03
  • I've done this one filter_var($s_email, FILTER_SANITIZE_EMAIL); Commented Jun 26, 2017 at 18:04
  • @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. Commented Jun 26, 2017 at 18:17
  • Because that is a valid email, so you only want to accept certain valid emails... Commented Jun 26, 2017 at 18:21

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain this part [^@]*@
Yes, it means any character except a @ up to a @.
Need i modifier if you're not using A-Z
It wasn't significant to note but I have set it in live demo already. @AbraCadaver
1

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.

1 Comment

You are ignoring email addresses with dots . and dashes - in user/domain names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.