Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Created August 14, 2018 08:03
Show Gist options
  • Save fyulistian/04deb27730c32fe99e87a1e26b6b3449 to your computer and use it in GitHub Desktop.
Save fyulistian/04deb27730c32fe99e87a1e26b6b3449 to your computer and use it in GitHub Desktop.
Thousand separator with decimal on typing
var element = '#numberFormattedWithDecimal';
$(element).keyup(function(event) {
/**
* Regular Expression.
* ref.
* https://regex101.com
*
* @type {RegExp}
*/
var regex = /([0-9])([0-9]{2})$/;
var rgex = /\D/g;
var regx = /\B(?=(\d{3})+(?!\d)\.?)/g; //
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(rgex, "")
.replace(regex, '$1.$2')
.replace(regx, ",");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment