Last active
October 13, 2018 12:49
-
-
Save gknasln/82f21206219797ce965eea08bf27bbe5 to your computer and use it in GitHub Desktop.
Untitled
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{ | |
width: 200px; | |
height: 20px; | |
padding-left: 10px; | |
margin: 50px; | |
} |
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
<div class="container"> | |
<input type="text" > | |
</div> |
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
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; | |
} | |
} | |
}); |
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
{"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