Javascript validation to allow the only numeric value in the text field.

How to set the validation to allow only numeric value in the HTML text field using JAVASCRIPT. We can set the validation using window keycode event on JAVASCRIPT. Like we want to allow the user only able to enter the numeric value in the text field just we need to set JAVASCRIPT function called on KeyPress like below.

Let’s create the textbox for set validation.

<input type="text" name="text" onkeypress="return thecodingstuffNumberValue(event)">

Now let’s create the validation function for allowing user to enter the only numeric value.


        function thecodingstuffNumberValue(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 31 && (charCode  57)) {
                return false;
            }
            return true;
        }
    

It’s too easy to set the numeric validation for textbox field using javascript.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s