<input type="text" onchange="setValidParValue(this)"> <script> function setValidParValue(elem) { elem.value = Number.parseInt(elem.value.replace(/[^0-9]/g, '')).toString(); } </script>