Forked from pielgrzym/jquery.input_formatter.js
Last active
December 6, 2017 12:43
-
-
Save RicardoJorge/8525653 to your computer and use it in GitHub Desktop.
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
(function($){ | |
$.fn.extend({ | |
formatInput: function(settings) { | |
var $elem = $(this); | |
settings = $.extend({ | |
errback: null | |
}, settings); | |
$elem.bind("keyup.filter_input", $.fn.formatEvent); | |
}, | |
formatEvent: function(e) { | |
//split with the "." because of the decimal value | |
var valueDecimal = initial_value.split("."); | |
//formats the part without the decimal part | |
var formatedValue = $.fn.insertSpaces(valueDecimal[0]); | |
var finalValue; | |
//checks if it has decimal values or not | |
if (valueDecimal.length > 1) { | |
finalValue = formatedValue + "." + valueDecimal[1]; | |
} else { | |
finalValue = formatedValue; | |
} | |
elem.val(finalValue); | |
//I had to add this line because I'm working with knockout, and it wouldn't update the value on IE | |
elem.trigger('change'); | |
}, | |
insertSpaces: function(number) { | |
return number.replace(" ", "").replace(/(\d)(?=(?:\d{3})+$)/g, "$1 "); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment