Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active December 18, 2015 00:18
Show Gist options
  • Save mariocesar/5695113 to your computer and use it in GitHub Desktop.
Save mariocesar/5695113 to your computer and use it in GitHub Desktop.
#javascript #jquery #trim I get tired of removing the blank spaces everywhere, ... If I just realize months ago that I can do this !
// Load after jquery.js
// Instead of $('#my_input').val(), do $('#my_input').trimmed_val()
(function() {
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, '');};
String.prototype.ltrim = function(){return this.replace(/^\s+/,'');};
String.prototype.rtrim = function(){return this.replace(/\s+$/,'');};
String.prototype.fulltrim = function(){
return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
};
$.fn.trimmed_val = function(value) {
if ( !arguments.length ) {
return $.fn.val(value).fulltrim();
} else {
return $.fn.val(value.fulltrim());
}
}
}($));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment