| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Wednesday, March 31, 2010

Simple JQuery Form Sanitation

So I'm taking my first stab at publishing code based on JQuery. To make this code work, you need to have the JQuery script added to your page which is as easy as adding this block of code to the <HEAD> of your page:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
Once you've added this line to your page, you can add the following code:
<script type='text/javascript'>
$(document).ready(function(){ $(':input').blur(function() { $(this).val($(this).val().replace(/<[^>]*>/g,'')); }); });
</script>
Great, so what the heck does that do? This code will remove any HTML tags from user input on your site so if you want some HTML input, you'll have to modify my code. Otherwise this will go a long way towards preventing injection attacks on your site.

0 comments: