Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

542
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!