Last Updated : Jan 1, 2023
So it will checks when user press any key on input field if they entered numeric value it allows, otherwise it won’t allow user to enter any key.
Here we defined input field with onkeypress event for initialize isNum(event) function call and within function when user press any key this function loaded and checks entered key is numeric value or not.
If pressed key except numeric value this function not allow user to enter on input field otherwise only it allows user to enter on input field those numeric values. Here we validated when we press any inputs on keyboard.
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined <h3> tag for instruct user to give input as numeric values and input field defined for collects user inputs. In input field we defined ‘onkeypress’ event with isNum() function.
- In script we defined function isNum(event) for verify users any key press on keyboard.
- When user press any key on keyboard it will checks whether pressed key is numeric value by key value.
- In keyboard each key had one value here we collects each key value when user press any key if entered key value greater than 31 and less than 48 or greater than 57 then those are referred characters value so it won’t allow user to enter on input field.
- If user pressed any numeric value then only it allows user to enter on input field.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Here we can only give numeric value as input on input field except numeric values in keyboard user pressed other keys it won’t allow user to enter any data on input field.
This is onkeypress event so it will verifies any input when user pressing on keyboard.