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

163
Views
how to prevent adding scripts in input fields

I need to prevent adding scripts inside input fields.is there any way to prevent adding javascript codes in text fields/text areas?

function filter($event) {
    var regex = /[^a-zA-Z0-9_]/;
    let match = regex.exec($event.target.value);
    console.log(match);
    if (match) {
        $event.preventDefault();
    } else {
        return true;
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can sanitize the input by defining the blacklist regex which contains the patterns not allowed by the input and then replaced the part of input string with empty string if matched with the blacklist regex.

For now I just added a simple blackList regex (You can modify it as per your requirement) which will replace all the text comes between < and >. For Ex: If user enter <script>Hello</script> (This whole input text will get replaced with the empty string on keyup event.

const blackList = /<+>/ig

function sanitizeInput() {
  const inputStr = document.getElementById('inputStr').value;
  console.log('inputStr', inputStr)
  document.getElementById('result').innerHTML = inputStr?.replace(blackList, '')
}
<input type="text" id="inputStr" onkeyup="sanitizeInput()"/>

<div id="result"></div>

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!