JavaScript
Article

jQuery Strip Harmful Characters from String

By Sam Deering

jQuery Function to strip out all potentially harmful characters from an input field. Useful for extra security measures of filtering requests to your server before using AJAX for example.

Also see: 10 jQuery Security Plugins

/**
 * Strip out all potentially harmful characters from an input field
 * @param {String} str
 * @returns {String}
 */
filterInputText = function(str)
{
	try
	{
		return str.replace(/s+/gm, ' ').match(/[a-zA-Z0-9(), .!/:%@&?+_=-$]+/gm).join('');
	}
	catch(e)
	{
		return '';
	}
}

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.