1function checkKey() {
2 var clean = this.value.replace(/[^0-9,]/g, "")
3 .replace(/(,.*?),(.*,)?/, "$1");
4 // don't move cursor to end if no change
5 if (clean !== this.value) this.value = clean;
6}
7
8// demo
9document.querySelector('input').oninput = checkKey;