javascript sanitize input slug 1

javascript sanitize input slug

	// https://stackoverflow.com/a/1054862/485063
	jq('.input.slug, .slug, input .slug').unbind().bind('keyup paste', function(){
		var low=	jq(this).val().toLowerCase()
			.replace(/ /g,'-')
			.replace(/\_/g,'-')
			.replace(/[^\w-]+/g,'')
			.replace(/\-\-+/g,'-');
		
		jq(this).val(low);
	});

Here is what the above code is Doing:
1. When the user types in the input field, the value is converted to lowercase.
2. All spaces are replaced with hyphens.
3. All underscores are replaced with hyphens.
4. All non-word characters are removed.
5. All multiple hyphens are replaced with a single hyphen.

Similar Posts