Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent users from writing some specific words in a textbox - JQuery

Is it possible to prevent users from typing or using some specific words in a textbox using JQuery or Javascript? For example, I have a textbox and I don't want users to use words like 'Phone', 'Home', 'Address' etc. Please Help.

like image 253
Ajmal Razeel Avatar asked Jul 28 '16 13:07

Ajmal Razeel


1 Answers

This might be an option.

var txtBox = $('#text-box');
var blackList =  ['Phone', 'Home', 'Address'];

function checkBlackList(str) {
  $.each(blackList, function(i, n) {
    if(new RegExp(n, "i").test(str)) {
      txtBox.val(txtBox.val().replace(new RegExp(n, "gi"), "xxx"))
    }
  })
}

txtBox.on('keydown', function(e) {
  checkBlackList(this.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<textarea name="" id="text-box" cols="30" rows="10"></textarea>
like image 105
DavidDomain Avatar answered Sep 21 '22 04:09

DavidDomain