Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
My custom Regex email validator is not working as intended

I am trying to create a custom Regex to validate emails that follow the rules stated below:

  1. Email should start with a alphabet (a-z) and should not start with special characters or numeric values
  2. after the first letter, it could contains numbers(0-9), letters(a-z), underscores(_) and dot(.)
  3. it should have only one @ symbol in it
  4. after the @ symbol, it should only allow to contain alphabets and dot (no more special characters or numbers other than dot(.))
  5. email should not end with dot(.) but rather end with an alphabet
  6. it should not have any whitespace anywhere

I have two arrays: trueEmails contains valid emails and notEmails contains invalid emails.

I created the following Regex:

const email = /^[a-zA-Z]+(\d|.|\w)*@[a-zA-Z]+.[a-zA-Z]+.*[a-zA-Z]+$/;

My Regex is not working for rule no. 2, 3, 4 and 6. Here is my code.

const notEmails = [
    // rule 1
    '_test@email.com',
    '#test@email.com',
    '1test@email.com',
    // rule 2
    'test&131@yahoo.com',
    // rule 3
    'test@gmail@yahoo.com',
    // rule 4
    'test@yahoo23.com',
    // rule 5
    'test@yahdsd.com.',
    // rule 6
    'white space@gmail.com'

]

const trueEmails = [
    // rule 1
    'test@email.com',
    // rule 2
    'test2email@yahoo.com',
    'test_email@yahoo.com',
    'test.123_.emai.l@yahoo.com',
    // rule 3
    'test@gmail.com',
    // rule 4
    'testsample@yahoo.co.in',
    // rule 5
    'testdample232@gmail.com',
    // rule 6
    'no_white_space@gmail.com'
]

const email = /^[a-zA-Z]+(\d|.|\w)*@[a-zA-Z]+.[a-zA-Z]+.*[a-zA-Z]+$/;

console.log("NotEmails, should return false")
console.log(notEmails.map((each) => each + ' => ' + email.test(each)));

console.log("trueEmails, should return true")
console.log(trueEmails.map((each) => each + ' => ' +  email.test(each)));

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've updated the regex to work for you. It's not fool proof but works for the restrictions you've put in place.

const notEmails = [
    // rule 1
    '_test@email.com',
    '#test@email.com',
    '1test@email.com',
    // rule 2
    'test&131@yahoo.com',
    // rule 3
    'test@gmail@yahoo.com',
    // rule 4
    'test@yahoo23.com',
    // rule 5
    'test@yahdsd.com.',
    // rule 6
    'white space@gmail.com'

]

const trueEmails = [
    // rule 1
    'test@email.com',
    // rule 2
    'test2email@yahoo.com',
    'test_email@yahoo.com',
    'test.123_.emai.l@yahoo.com',
    // rule 3
    'test@gmail.com',
    // rule 4
    'testsample@yahoo.co.in',
    // rule 5
    'testdample232@gmail.com',
    // rule 6
    'no_white_space@gmail.com'
]

const email = /^[a-zA-Z]+[a-zA-Z0-9_.]+@[a-zA-Z.]+[a-zA-Z]$/;

console.log("NotEmails, should return false")
console.log(notEmails.map((each) => each + ' => ' + email.test(each)));

console.log("trueEmails, should return true")
console.log(trueEmails.map((each) => each + ' => ' +  email.test(each)));

Description

Regex: /^[a-zA-Z]+[a-zA-Z0-9_.]+@[a-zA-Z.]+[a-zA-Z]$/

  • ^ Start of line
  • [a-zA-Z] any character from a-z
  • + one or more times
  • [a-zA-Z0-9_.] any character from a-z, and number, underscores and periods.
  • + one or more times
  • @ matches literal @ sign
  • [a-zA-Z.]+ any character from a-z and periods
  • + one or more times
  • [a-zA-Z]` any character from a-z
  • $ end of line
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda