Last active
December 18, 2015 00:18
-
-
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 !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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