In the given example, we are going to show you how to restrict the user to enter any number into the input field. For this purpose, we have created a text field. On pressing the key to write the text on the textbox, you will see that it will take only characters, not the numbers. This is possible by using the onkeypress event. It occurs when a keyboard key is pressed or held down.
Here is the code:
<h2>Textbox showing onkeypress event</h2>
num = e.keyCode;
num = e.which;
chars = String.fromCharCode(num);
check = /\d/;
Enter text: <input type="text" onkeypress="return valid(event)"/>
On pressing the key, only characters get inserted in the text field:
Ask your questions, our development team will try to give answers to your questions.