A Pen by Rafael S. Meneses on CodePen.
Created
June 6, 2018 17:36
-
-
Save rtio/8a4e087a5fe1d57efe77c591cc75fd33 to your computer and use it in GitHub Desktop.
mask
This file contains hidden or 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 type='text' id="textBox"> |
This file contains hidden or 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
| String.prototype.formatTo = function(decimal) { | |
| let digits = this.replace(/\D/g, ''); | |
| digits = parseInt(digits).toString(); | |
| if (digits.length <= decimal) { | |
| return '00,' + ('0'.repeat(decimal) + digits).slice(-decimal); | |
| } | |
| if (digits.length == (decimal + 1)) { | |
| return '0' + digits.substring(0, 1) + ',' + digits.substring(1, decimal + 2); | |
| } | |
| if (digits.length > decimal) { | |
| return digits.substring(0, 2) + ',' + digits.substring(2, decimal + 2); | |
| } | |
| } | |
| let textBox = document.getElementById("textBox"); | |
| textBox.addEventListener("keyup", function(e) { | |
| this.value = this.value.formatTo(5); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment