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

142
Visualizações
RegEx Pattern Validations failing on html input

Issue: I'm trying to achieve the following results for the test cases using a Regex pattern on a input type text html.

Looking to meet the following criteria's:

  1. Numeric only - no alpha or special characters allowed

  2. No spaces between digits

  3. two digits after decimal is allowed

  4. Cannot be zero (0)

  5. 1st digit of the limit can not be zero (ex:01200)

    testPattern: RegExp = /(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$/;

Expected Results:

  • 0 //false
  • 012 // false
  • 0.00 //false
  • 0.0 //false
    1. //false
  • 00122 //false
  • 0.12 //true
  • 0.01 //true
  • 1,234.00 // true
  • 1.00 //true
  • 123,456 //true
  • 1.23 // true

TIA

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

0

You could assert not starting with a zero followed by only digits, or only dots, comma's and zero's till the end of the string.

^(?![0,.]*$|0\d*$)\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?$

Explanation

  • ^ Start of string
  • (?! Negative lookahead
    • [0,.]*$ Match optional zeroes , or . to the end of string
    • | Or
    • 0\d*$ Match 0 and optional digits to the end of string
  • ) Close lookahead
  • \d{1,3} Match 1-3 digits
  • (?:,\d{3})* Optionally repeat matching , and 3 digits
  • (?:\.\d{1,2})? Optionally match . and 1-2 digits
  • $ End of string

Regex demo

const regex = /^(?![0,.]*$|0\d*$)\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?$/;
[
  "0",
  "012",
  "0.00",
  "0.0",
  "0.",
  "00122",
  "0.12",
  "0.01",
  "1,234.00",
  "1.00",
  "123,456",
  "1.23"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));

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