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

250
Visualizações
Regular expression for format xx-xx (does not contain letters)

I am trying to use regex to check if the user input is in the correct format xx-xx (input only accepts numbers, does not accept alphanumeric characters)

I tried: /[1-9]{1,}\-[1-9]{1,}/ but when entering alphabetic characters still pass this test.

Can you guys help me. Thank.

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

0

Your regex is just fine, you just need to add positional asserts, "^" for the start of the string and "$" for the end of the string:

/^[1-9]{2}\-[1-9]{2}$/

It is better to put "{2}" if you only want xx-xx

about 4 years ago · Juan Pablo Isaza Relatório

0

/\d{2}-\d{2}/

Worked for me.

Breakdown:

\d checks for a digit character, i.e. 0-9.

{2} checks for two digits next to each other specifically.

- just checks for the hyphen character.

about 4 years ago · Juan Pablo Isaza Relatório

0

On the other hand, you can have several cases depending on what you are looking for precisely.

In case you agree that an xx-xx value can be 00-00 and any other value, you should use this regex instead:

/^\d{2}-\d{2}$/

In case you accept that an xx-xx value is never 00-00 but can be 01-03 and any other value, but never 00-00, you should use this regex (a bit long, sorry, but does the job perfectly):

/^[1-9](?=[0-9])[0-9]-[1-9](?=[0-9])[0-9]$|^[0-9](?=[1-9])[1-9]-[0-9](?=[1-9])[1-9]$/

Enjoy !

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