limiting the value that can be typed in the input

3

I have a php variable that has a value that varies depending on the time !! Example

1st moment $valor = 300

2nd moment $valor = 20

3rd moment $valor = 5300

4th moment $valor = 1300

I also have an input, this input can add any value as long as it is below the value at the moment of typing. Here's what I want to do !! If someone comes to enter a higher value in the input the value automatically changes to the maximum and not to what was typed !!

Example:

1st moment $valor = 300 || In that first moment the maximum value of the input can be only 300 !! But instead of 300 the user typed 301, at the same time I want these 301 to turn 300 as it is the limit value the user has!

    
asked by anonymous 13.02.2015 / 02:02

1 answer

6

Using jQuery I created this function that is listening for the keyup event (when the user releases a key). Within the function I check the value.

$('#txtValor').on('keyup', function(event) {
  var valorMaximo = 300;

  if (event.target.value > valorMaximo)
    return event.target.value = valorMaximo;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Digiteovalor:R$<inputtype="text" id="txtValor">
    
13.02.2015 / 02:39