1

In my form i have 13 input box in my form ,I allow user to type only text and number using following script

FILTER INPUT TEXT

$(function() {//<-- wrapped here
                $('.restrict').on('input', function() {
                    this.value = this.value.replace(/[^a-zA-Z0-9@]/g, ''); //<-- replace all other than given set of values
                });
            });

Above script allows only text from a-z, number only

My question is i need to filter certain words in input box before saving it to database

Eg: if user enters *adult* content in input box i need to filter the word adult and save only content to database

Can some one please help me

and can some one help me where do i find list of bad words

creator
  • 51
  • 15

5 Answers5

3

If it's a large number of words you need to filter, you're probably better off moving that logic from JS to PHP. Also, with JavaScript I can still manipulate the POST request to get bad words into your database.

Community
  • 1
  • 1
redburn
  • 542
  • 4
  • 24
  • is there a way to remove bad words in `html input box` – creator Dec 19 '14 at 16:29
  • With PHP you'd fetch the input from $_POST and then take out the bad words. If you want the input to be filtered in the browser, as soon as (or while) the words are entered, you'd have to use JavaScript. You marked the question with the PHP tag and you're storing the input in a database so I assumed the data would be processed by PHP. – redburn Dec 19 '14 at 16:36
1

try use replace()

have a look at @BBonifield answer for multiple words

http://jsfiddle.net/6hxspv14/2/

$(function(){

    var timer = 0;
    var sensor_word = ['adult', 'porn', 'sex'];

    $('.restrict').on('keyup', function(){

        clearTimeout(timer);
        var new_value = this.value;
        var new_sensor = $('.restrict').val();

        $.each(sensor_word, function (idx, word) {
          new_sensor = new_sensor.replace(word, '***');
        }); 

        timer = setTimeout(function(){
             $('#result').text( new_sensor );
        }, 0);

    });

    $('.restrict').on('keydown', function(){
        clearTimeout(timer);
    });


});
tonoslfx
  • 3,422
  • 15
  • 65
  • 107
0

You can accomplish it a few different ways, but one simple way would be to simply remove all the words that you don't want to include whenever the user leaves the field.

var badWords = [ 'adult', 'crap' ];
$(':input').on('blur', function(){
  var value = $(this).val();
  $.each(badWords, function(idx, word){
    value = value.replace(word, '');
  });
  $(this).val( value);
});
BBonifield
  • 4,983
  • 19
  • 36
0
$(function() { 
    $('.restrict').on('input', function() {

        var value = this.value;
        var isAdultContent = ( value.match( /^\W*adult\W+/ ) || [] ).length;
        var content = value.replace( /^\W*adult\W+/, '' );

        if( isAdultContent ){
            //...
        }
        else{
            //...
        }

    });
});
karkael
  • 431
  • 2
  • 9
0

The following might also be useful, check if value is in array using $.inArray :

$(function() {
$("#submit").click(function(e) {
    e.preventDefault();

    var badwords = ["doodoo", "peepee"];
    var inputVal = $('.restrict').val();

    if ($.inArray(inputVal, badwords) > -1) {  // returns -1 if value not in array
        $('.restrict').val('');   // empties input box if swear word is found
    }
    });
});

Check this fiddle: http://jsfiddle.net/jxvvqraL/

buydadip
  • 8,890
  • 22
  • 79
  • 154