Shiv Kumar Baghel
Shiv Kumar Baghel

Reputation: 2480

RegEx for email validation where atleast 2 char required before @

I'm trying below RegEx which need atleast 2 characters before @

^([a-zA-Z])[^.*-\s](?!.*[-_.@]{2})(?!.\.{2})[a-zA-Z0-9-_.]+@([\w-]+[\w]+(?:\.[a-z]{2,10}){1,2})$

like

NOT ALLOWED : [email protected]

NOT ALLOWED : [email protected]

NOT ALLOWED : [email protected]

SHOULD ALLOWED: [email protected]

SHOULD ALLOWED: [email protected]

SHOULD ALLOWED: [email protected]. (atleast one char after special char and before @)

SHOULD ALLOWED: [email protected]

SHOULD ALLOWED: [email protected]

Before @ only allowed special char . _ - which also not consecutively like (--) also not in beginning.

i tried below RegEx also but no luck

^[a-zA-Z)]([^.*-\s])(?!.*[-_.@]{2}).(?!.\.{2})[\w.-]+@([\w-]+[\w]+(?:\.[a-z]{2,10}){1,2})$

Upvotes: 1

Views: 955

Answers (1)

anubhava
anubhava

Reputation: 785521

I would suggest keeping things simple like this:

^([a-zA-Z][\w+-]+(?:\.\w+)?)@([\w-]+(?:\.[a-zA-Z]{2,10})+)$

RegEx Demo

By no means it is a comprehensive email validator regex but it should meet your requirements.

Details:

  • ^: Start
  • (: Start capture group #1
    • [a-zA-Z]: Match a letter
    • [\w.+-]+: Match 1+ of word characters or - or +
    • (?:\.\w+)?: Match an option part after a dot
  • ): End capture group #1
  • @: Match a @
  • (: Start capture group #2
    • [\w-]+: Match 1+ of word characters or -
    • (?:\.[a-zA-Z]{2,10})+: Match a dot followed by 2 to 10 letters. Repeat this group 1+ times
  • ): End capture group #2
  • $: End

Upvotes: 5

Related Questions