Java Script

Number Validation on textbox in JS

Spread the love

How to add number Validation on textbox in JS –

In this example, Number validation in Javascript “onkeypress” event to accept Number only.

  • The onkeypress=”return onlyNumberAccept(event,this);” function called in text box on onkeypress event.
  • No need for any jQuery library.
<!DOCTYPE html>
<html>
    <head>
    <script type="text/javascript">
    function onlyNumberAccept(e, t) {
        try {
            if (window.event) { var charCode = window.event.keyCode; }
            else if (e) { var charCode = e.which; }
            else { return true; }
            if ((charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 123) && charCode != 32 && charCode != 8 && charCode != 46) {
                return false;
            }
            return true;
        }
        catch (err) { alert(err.Description); }
    }
    </script>
   </head>
	<body>
     Mobile No. : <input type="text" maxlength="10" name="mobile_no" id="mobile_no" onkeypress="return onlyNumberAccept(event,this);">		
	</body>
</html>

More validations … Click here …

Number Validation on textbox in JS onkeypress

Leave a Reply

Your email address will not be published. Required fields are marked *