Last active
March 3, 2020 20:49
-
-
Save sidisinsane/509c6c72216830b6b4a2c34777b4e09d to your computer and use it in GitHub Desktop.
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
/** | |
* Format a number while typing it into an input field. | |
* | |
* @function formatNumberOnInput | |
* @param {!Element} element - The input element to format. | |
* | |
* @example | |
* | |
* formatNumberOnInput(document.getElementById('foo'); | |
*/ | |
function formatNumberOnInput(element) { | |
element.addEventListener('keyup', function(e) { | |
var number = parseInt(this.value.replace(/\D/g, ''), 10); | |
this.value = number.toLocaleString() !== 'NaN' ? number.toLocaleString() : ''; | |
}, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment