The input field accepts an integer or float. And this result is 24.00 or (24), but not higher than 24

$("#"+time_type_id+"_sun").keypress(function(e) {             
   if (e.which != 8 && e.which != 0 && e.which != 46 && parseInt($("#"+time_type_id+"_sun").val()) > 25 && (e.which < 48 || e.which > 57)) {
     return false;
  }            
}); 

      

How do I find the total input is less than 25. And the Text field takes an integer or float. If it is the maximum input length of integers is 2 (example: - 24) or if it is equal to the float the maximum input length is 5 (example: - 24.00).

+3


source to share


2 answers


You are going on this wrong. Instead of handling the event, keypress

you can handle the event input

(which concerns any changes made to the content of the text box) and then check if the value entered is greater than the numeric value 24:

$("input").on('input', function() {
  var value = +this.value;

  if (typeof value != "number" || value > 24) {
    $(this).val(24);
  }
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="number" placeholder="Enter a number here" />
      

Run code




Here, if you enter a value that is not numeric or greater than 24, the value will be forced to 24. If you want to force the value 24.00

, you can use $(this).val('24.00');

.

For reference, +this.value

uses Unary Plus to convert this.value

to a numeric value (similar to how it works parseInt

).

+1


source


You can try this:



if ((e.which != 8 && e.which != 0 && e.which != 46) && ((e.which < 48 || e.which > 57) || parseInt($("#"+time_type_id+"_sun").val()) > 25)) {
        return false;
}      

      

0


source







All Articles