Skip to content

Instantly share code, notes, and snippets.

@gknasln
Last active October 13, 2018 12:49
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 gknasln/82f21206219797ce965eea08bf27bbe5 to your computer and use it in GitHub Desktop.
Save gknasln/82f21206219797ce965eea08bf27bbe5 to your computer and use it in GitHub Desktop.
Untitled
input{
width: 200px;
height: 20px;
padding-left: 10px;
margin: 50px;
}
<div class="container">
<input type="text" >
</div>
document.querySelector('input').addEventListener('keydown', function(e){
e.preventDefault();
if(e.keyCode > 48 && e.keyCode < 57){
var value = this.value.replace(/,/g, "");
value += e.key;
const newValue = value.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
this.value = newValue;
}else{
if(e.key == 'Backspace'){
var value = this.value.replace(/,/g, "");
value = value.substr(0, value.length-1);
const newValue = value.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
this.value = newValue;
}
}
});
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"javascript"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment