Skip to content

Instantly share code, notes, and snippets.

@sskim91
Created December 31, 2023 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sskim91/1db3b4813776fe24ee3b36a598d21ce0 to your computer and use it in GitHub Desktop.
Save sskim91/1db3b4813776fe24ee3b36a598d21ce0 to your computer and use it in GitHub Desktop.
Add comma/dot to number input on keyup event
$('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