«

»

Print this Post

Strip bad characters from textarea as you type with jquery.

<script language='javascript'>
/*
* This function does some client side input validation. Automatically removing
* Bad characters before they cause a problem. Additional validation is done on the
* server.

* We're also hacking the onpaste event.
*/
$(document).ready(function(){
//set keyup event
$("body").keyup(function(){
if ($("#body").val().match(/[^a-zA-Z0-9 n.,?]/g)) {
$("#body").val($("#body").val().replace(/[^a-zA-Z0-9 n.,?]/g, ''));
}//end if
}); // end keyup

$(document).bind('paste', function(e){
if ($("#body").val().match(/[^a-zA-Z0-9 n.,?]/g)) {
$("#body").val($("#body").val().replace(/[^a-zA-Z0-9 n.,?]/g, ''));
}//end if
})

});// end docready
</script>

Permanent link to this article: http://www.lukemacneil.com/2010/04/23/strip-bad-characters-from-textarea-as-you-type-with-jquery/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>