Created
December 31, 2023 08:52
-
-
Save sskim91/1db3b4813776fe24ee3b36a598d21ce0 to your computer and use it in GitHub Desktop.
Add comma/dot to number input on keyup event
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
$('input.number').val(function(index, value) { | |
return value | |
.replace(/\D/g, "") | |
.replace(/\B(?=(\d{3})+(?!\d))/g, ",") | |
; | |
}).keyup(function(event) { | |
// skip for arrow keys | |
if(event.which >= 37 && event.which <= 40) return; | |
// format number | |
$(this).val(function(index, value) { | |
return value | |
.replace(/\D/g, "") | |
.replace(/\B(?=(\d{3})+(?!\d))/g, ",") | |
; | |
}); | |
}); | |
//https://stackoverflow.com/a/2632502/794445 | |
//<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> | |
//<input class="number"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment