Without further ado, here’s the code: Copy code The code is as follows: < title> <br>/**<br>* Only numbers 0-9 can be entered <br>*/ <br>function kp(){ <br>if(event.keyCode< =47 || event.keyCode>=65) { <br>event.returnValue=false; <br>} <br>} <br>/**<br>* Only numbers 0-9 and . (decimal point) can be entered <br>*/ <br>function kpd(){ <br>if(event.keyCode<=47 || (event.keyCode>=65 && event.keyCode!=190)) { <br>event.returnValue=false; <br>} <br>} <br>< /script> <br></head> <br><body> <br>This input can only input 0-9<input type="text" id="t1" onkeydown="kp()"/> ;<br/> <br>This input can only input 0-9.<input type="text" id="t2" onkeydown="kpd()"/> <br></body> <br></html> <br> </div> <br>Truncate the input event directly during keydown, and prevent the loss at all. <br><br>Don’t use the awkward method of replace - enter the letters and then replace them, it’s so ugly!