Skip to content

Instantly share code, notes, and snippets.

@pielgrzym
Created June 25, 2011 23:07
Show Gist options
  • Save pielgrzym/1047003 to your computer and use it in GitHub Desktop.
Save pielgrzym/1047003 to your computer and use it in GitHub Desktop.
Simple plugin to format numbers in input fields on the fly (like: 10000 -> 10 000)
(function($){
$.fn.extend({
formatInput: function(settings) {
var $elem = $(this);
settings = $.extend({
errback: null
}, settings);
$elem.bind("keyup.filter_input", $.fn.formatEvent);
},
formatEvent: function(e) {
var elem = $(this);
var initial_value = elem.val();
elem.val($.fn.insertSpaces(initial_value));
},
insertSpaces: function(number) {
return number.replace(" ", "").replace(/(\d)(?=(?:\d{3})+$)/g, "$1 ");
}
});
})(jQuery);
@qoolek
Copy link

qoolek commented Nov 27, 2013

cool, but not support decimal like 10000.00 =(

@RicardoJorge
Copy link

Hi, I just created this account because I used your code and found some cool ways to improve it.

I'm really new to Git, so I don't know what's the best way to pass the code up to you.

I forked your gist and altered it the way I have it on my project.

Here is the link for it: https://gist.github.com/RicardoJorge/8525653

If there is a better way to push the code to you (in case you like it), please let me know.

And thank you for that code, it was really was exactly what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment