jquery - javascript profanity filter on a contenteditable div -
i working on site filters bad words on array. whenever user types word on textbox, alert box appears if word array found. i'd same instead of using textbox, i'd use contenteditable div. i'll needing jquery on there seems conflict on code since initial code done in pure js.
fiddle link: http://jsfiddle.net/br7td/
here's javascript:
var swear_words_arr=new array("bad","evil","freak"); var swear_alert_arr=new array; var swear_alert_count=0; function reset_alert_count() { swear_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.form1.user_text.value; for(var i=0; i<swear_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).tolowercase()) { swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length)); swear_alert_count++; } } } var alert_text=""; for(var k=1; k<=swear_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1]; } if(swear_alert_count>0) { alert("please refrain using offensive words"); /* + alert_text */ document.form1.user_text.select(); } else { document.form1.submit(); } } function select_area() { document.form1.user_text.select(); } window.onload=reset_alert_count;
i'm pretty new this. appreciated.
try
<div id="textarea"> <span id="myinput" contenteditable="true">kfjdkfj</span> </div>
and
var swear_words_arr=new array("bad","evil","freak"); var regex = new regexp('\\b(' + swear_words_arr.join('|') + ')\\b', 'i' ); function validate_user_text() { var text = $('#myinput').text(); if(regex.test(text)) { alert("please refrain using offensive words"); /* + alert_text */ $('#myinput').focus(); return false; } }
demo: fiddle
a more jquery solution can found here
Comments
Post a Comment