[RESOLVED]How to restrict user to input more than 70 as a number

i have textbox and i want user will not be able to write more than 70. if the value is 70 then it is ok but if the value is 71 then user will not be able put 71. how to achieve it with JavaScript or Jquery based on key-press event for a specific textbox…..any
idea? thanks

There are a few approaches that you can use to handle this but the easiest might be to consider using an actual plug-in like autoNumeric to handle this, which might look like the following
to implement :

<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <!-- jQuery and Plugin References -->
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script src="http://www.decorplanit.com/plugin/autoNumeric-1.9.18.js"></script>
  <script type='text/javascript'>
    $(function($) {
      // Only allow integer values with a max of 70 (denoted by the vMax parameter)
      $('.numericOnly').autoNumeric('init', { vMax: 70, lZero: 'deny', aSep: '', mDec: 0 });    
    });  
  </script>
</head>
<body>
  <!-- Define your specific properties for your field -->
  <input type="text" class="numericOnly" >
</body>

You can see a working example here.

You can also use the below Jasvscript function to restrict user from entering value above 70.

<script type="text/javascript">
    function minmax(value, min, max) 
    {
        if(parseInt(value) < 0 || isNaN(value)) 
            return 0; 
        else if(parseInt(value) > 70) 
            return 70; 
        else return value;
    }
</script>

Complete Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
 <script type="text/javascript">
    function minmax(value, min, max) 
    {
        if(parseInt(value) < 0 || isNaN(value)) 
            return 0; 
        else if(parseInt(value) > 70) 
            return 70; 
        else return value;
    }
</script>
</head>
<body>

<input type="text"  id="txtnumbers" maxlength="2" onkeyup="this.value = minmax(this.value, 0, 70)"/>
</body>
</html>

Source URL

thanks for the answer. please explain the meaning of these parameter lZero:
‘deny’, aSep:
, mDec:
0

Sorry Rion i could not use the js file jquery-1.11.0.min.js you said because i was using old jquery file version is 1.4.1. do u have any solution for old jquery file? thanks

<script type="text/javascript">
function validate() {
var FieldVal = document.getElementById('FieldName').value;
if(FieldVal < 51) {
alert("Valid!");
} else {
alert("Invalid");
}
}
</script>
<input id="FieldName" type="text" name="MyField" onKeyUp="validate()" MaxLength="2" /

Try the above method , see here : https://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/402428/max-value-enter-in-a-textbox-using-javascript

And if you are using HTML 5 you can do like this :

<input type="number" name="quantity" min="1" max="5">

See here:

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_max_min

Leave a Reply