Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
Restricting minimum length of email ONLY for Gmail logins Regex

So, there is a task to match emails in an input agains some certain conditions. Most of them are fulfilled. All of them, actually, except one.

So, user emails have to be up to 30 characters long, but they cannot be shorter than 6 only for gmail users (the part before the @). Other types of email can have even shorter emails.

Please help me find a correct regexp!

My current one is:

^((?!.*?\.\.)[a-zA-Z0-9\.]{1,30})\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using a case insensitive match, you could write the pattern as:

^(?!.*?\.\.)(?:[a-zA-Z0-9.]{1,30}@(?!gmail\.com$)(?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}|[a-zA-Z0-9.]{6,30}@gmail\.com)$

The pattern matches:

  • ^ Start of string
  • (?!.*?\.\.) Assert not .. to the right
  • (?: Non capture group for the 2 alternatives
    • [a-z0-9.]{1,30} Match 1-30 chars specified in the character class
    • @(?!gmail\.com$) Match @ and assert not followed by gmail.com
    • (?:[a-z0-9-]+\.)+[a-z0-9]{2,4} Match 1+ repetitions of the character class and . and match 2-4 chars of the second character class
    • | Or
    • [a-zA-Z0-9.]{6,30}@gmail\.com
  • ) Close non capture group
  • $ End of string

Regex demo

const regex = /^(?!.*?\.\.)(?:[a-zA-Z0-9.]{1,30}@(?!gmail\.com$)(?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}|[a-zA-Z0-9.]{6,30}@gmail\.com)$/i;
[
  "testing@test.com",
  "t@gmail.com",
  "t@test.com",
  "testing@gmail.com"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));

A bit shorter way to write it could be asserting not 1-5 characters before gmail.com:

^(?!.*?\.\.)(?![a-z0-9.]{1,5}@(?=gmail\.com$))[a-z0-9.]+@(?:[a-z0-9-]+\.)+[a-zA-Z0-9]{2,4}$

Regex demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!