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

545
Visualizações
prevent specific character input jquery

I have an input field like below

<input type='text' class='keywordInput' value='' />

I want to restrict specific special characters to be input. I have written following code snippet but it does not work for the first time restricted character is input.

$(".keywordInput").on("keypress", function (e) {
  var pattern = /[&/\\#,+(\);@$<>?^~%":\\*?<>|[\]'\{\}]/g;
  if (pattern.test(e.currentTarget.value)) {
    return false
  }
});

I have created jsfiddle demo here. you can try pressing > it will input this character once and will not allow to write any character (even alphanumeric) afterwards.My Goal is to prevent inputing > on the first time also & I want to achieve it using jquery

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

0

var pattern = /[&/\\#,+(\);@$<>?^~%":\\*?<>|[\]'\{\}]/g;

const onKeydown = event => {
  const match = event.key.match(pattern)
  if (match !== null) {
    event.preventDefault()
  }
}

document.querySelector('.keywordInput').addEventListener('keydown', onKeydown)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<input type='text' class='keywordInput' value='' />

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