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

111
Visualizações
Javascript limit input to specific numbers

I wanted to create an input field for a postal code check, i found number ranges to not work, as they arent all sequential:

In Dresden one postal code is 01157 but the next one is 01169, so a simple range won't do.

Is it possible to have all the necessary zip codes stored and to then compare if the input is one of these and if yes, then it will be valid?

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

0

So here is a minimal solution for a basic understanding of how this works in principle.

I'm using an event listener, so whenever the input value changes, a check is done. If the provided value is included in the list of valid numbers, the input is colored green, otherwise red.

The submit button is only enabled if a valid input was provided.

const plzList = [1, 4, 9, 185, 2164]

const plzInput = document.getElementById('plzInput')
const submit = document.getElementById('submit')

plzInput.addEventListener('input', e => {
  const plzProvided = e.target.value
  const isValid = plzList && plzList.includes(parseInt(plzProvided))
  e.target.style.backgroundColor = isValid ? 'green' : 'red'
  submit.disabled = !isValid;
});
<input id="plzInput" type="number" />
<input id="submit" type="submit" disabled/>

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