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

617
Visualizações
Regex for if a number contains a decimal point, it must have a decimal

There is another question, which deals with a mandatory decimal place, in this case, it is optional, and only if there are decimals. I need to check to see if a number is valid in a numeric input by comparing it to my regex on paste and keyed in. The number can contain up to 9 numbers, and if it has a decimal point, up to 6 decimal places.

For example:

123456789.123456

is a valid number, but

1234567890.1234567

or

1234567890.

is not valid. My regex thus far is:

/^(\d{0,9})(\.{0,1}\d{0,6})*$/

..but it still allows a decimal place without decimals.

RegExr link

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

0

Use

^\d{0,9}(?:\.\d{1,6})?$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \d{0,9}                  digits (0-9) (between 0 and 9 times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d{1,6}                  digits (0-9) (between 1 and 6 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
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