Created
August 14, 2018 08:03
-
-
Save fyulistian/04deb27730c32fe99e87a1e26b6b3449 to your computer and use it in GitHub Desktop.
Thousand separator with decimal on typing
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
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