All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Only Number Validation In JavaScript Onkeypress

Last Updated : Jul 1, 2023

Only Number Validation In JavaScript Onkeypress

In this tutorial we will show you the solution of only number validation in JavaScript onkeypress, here we defined input tag with attributes of ‘onkeypress,placeholder’, placeholder used for instruct user what input needs to give on input field, onkeypress event will works for all keys except alt, ctrl, shift and esc.

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.

Step By Step Guide On Only Number Validation In Javascript Onkeypress :-

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>
<html>
    <head>
        <title>Only Numbers OnKeyPress</title>
    </head>
    <body>
        <h3>THIS INPUT FIELD ALLOWS NUMBERS ONLY</h3>
        <input type="text" onkeypress="javascript:return isNum(event)" placeholder="Enter Phone Number" name="ph">
        <script>
            function isNum(evt){
                var charCode=(evt.which)?evt.which:event.keyCode
                if(charCode>31 && (charCode<48 || charCode>57)){
                    return false;
                }
                else{
                    return true;
                }
            }
        </script>
    </body>
</html>
  1. <!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.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. 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.
  4. 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.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. 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.
  7. In script we defined function isNum(event) for verify users any key press on keyboard.
  8. When user press any key on keyboard it will checks whether pressed key is numeric value by key value.
  9. 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.
  10. If user pressed any numeric value then only it allows user to enter on input field.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to validate using onkeypress event using javascript. When we executes our program on browser we can see one input field.

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.

I hope this tutorial on only number validation in JavaScript onkeypress helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪