Welcome to my blog, hope you enjoy reading
RSS

Sunday 23 February 2014

Allow only Numeric in HTML inputbox

Allow only Numeric in HTML inputbox Following code is used to allow only numeric (0-9).

<script language="Javascript">
      function isNumber(evt) {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
 
         return true;
      }
</script>
 
<input onkeypress="return isNumber(event)" type="text">


Another Way

Following code is used to allow only numeric (0-9).
   
<script language="Javascript">
     function validate(evt) {
      var theEvent = evt || window.event;
      var key = theEvent.keyCode || theEvent.which;
      key = String.fromCharCode( key );
      var regex = /[0-9]|\./;
      if( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
      }
    }
</script>
 
<input onkeypress="return isNumber(event)" type="text">

0 comments: