use regex.
<input id="inputBox" name="inputBox" />
<script type="text/javascript">
var inputBox = document.getElementById('inputBox');
inputBox.onchange = function(){
inputBox.value = inputBox.value.replace(/[^0-9]/g, '');
}
</script>
or you can use a timer to constantly check this field.
<input id="inputBox" name="inputBox" />
<input id="inputBox2" name="inputBox2" />
<script type="text/javascript">
var timer = new Array();
function checkFields(el){
var inputBox = document.getElementById(el);
inputBox.value = inputBox.value.replace(/[^0-9]/g, '');
clearTimeout(timer[el]);
timer[el] = setTimeout((function(){ checkFields(el); }), 50);
};
function timerFields(el){
timer[el] = setTimeout((function(){ checkFields(el); }), 50);
};
timerFields('inputBox');
timerFields('inputBox2');
</script>
source
share