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

90
Visualizações
How can I restrict form input to specific characters with a regex?

I have a made a regex for user to only allow these characters to be input. If user writes for example '@' it should not allow it, but in my code if user press @ and than press the tab the '@' still remains there.

function onlyAlphabet(inputVal) {
  var patt = /^[a-zA-Z.123456789]+$/;
  
  if (patt.test(inputVal)) {
    document.getElementById('facebook').value = inputVal;
  } else {
    var txt = inputVal.slice(0, -1);
    document.getElementById('facebook').value = txt;
  }
}
<div class="form-group mb-3">
  <label for="exampleFormControlTextarea1" class="fw-bold my-1 fs-7" id="text-color1">Facebook</label>
  <input type="text" placeholder="Facebook" onkeypress="onlyAlphabet(value)" id="facebook" class="form-control" name="facebook" value="<?php echo $row['facebook'] ?>" style="padding-left: 0 !important; border: none; border-bottom: 1px solid #E8E8E8; border-radius: 0 !important;">
</div>

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

0

You should add onchange="onlyAlphabet(value)" and onblur="onlyAlphabet(value)" to make sure it would be called in any circumstances

about 4 years ago · Juan Pablo Isaza Relatório

0

you could do it using any of these 3 ways -

    1. onChange="onlyAlphabet(value)" - when the value of the input changes
    1. onKeyUp="onlyAlphabet(value)" - or onKeyDown, when a key is released/pressed
    1. onBlur="onlyAlphabet(value)" - when the focus leaves the input...
about 4 years ago · Juan Pablo Isaza Relatório

0

Must set oninput instead of onkeypress

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