You will need events to check if the entered text is a number or not. See Code Example
Js
function isValidChar(char){ var txt = char; var found = false; var validChars = "0123456789"; //List of valid characters for(j=0;j<txt.length;j++){ //Will look through the value of text var c = txt.charAt(j); found = false; for(x=0;x<validChars.length;x++){ if(c==validChars.charAt(x)){ found=true; break; } } if(!found){ //If invalid character is found remove it and return the valid character(s). document.getElementById('txtFld').value = char.substring(0, char.length -1); break; } } }
HTML
<input type="text" id="txtFld" onKeyup="isValidChar(this.value);" />
See the working code here jsfiddle
Although the code is a bit long, you can minimize it if you know how to use regex or jquery.
source share