Home Javascript How to use Javascript to force numeric value in textbox
How to use Javascript to force numeric value in textbox

Using Javascript is a great way to force user to only enter numeric data into the textbox. Often we have many ways to validate the input data. Validation can be done on the client side or the server side. However I would suggest that we perform client side validation as this will save the page posting back to the server for validation.

In this example you will learn how to use Javascript to prevent user from entering non numeric data into the textbox.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Untitled Page</title>

</head>

<body>

 

    <script language="Javascript" type="text/javascript">    

        function isNumericKey(evt)

        {

            var charCode = (evt.which) ? evt.which : event.keyCode

            if (charCode > 31 && (charCode < 48 || charCode > 57))

                return false;

 

            return true;

        }    

    </script>

 

    <input id="txtNoRooms" onkeypress="return isNumericKey(event)" type="text" name="txtNoRooms" />

</body>

</html>



Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

"