How to limit an input according to the variable?

Asked

Viewed 169 times

2

I have 2 field as shown below :

 <div class="col-md-4">
                      <label for="descricao">Valor título </label>
                          <div class="input-group">
                            <span class="input-group-addon">R$</span>
                            <input type="text" id="entr_valorMoeda" class="form-control valor" value="<?php echo $conta->ENTR_ValorMoeda;?>" readonly >
                            <input type="hidden" class="valor"  name="entr_valorMoeda" value="<?php echo $conta->ENTR_ValorMoeda;?>">                                        
                          </div>
                  </div>

                  <div class="col-md-4">
                      <label for="descricao">Valor baixa </label>
                        <div class="input-group">
                          <span class="input-group-addon">R$</span>
                          <input type="text" class="form-control valor" id="valorBaixa" name="valorBaixa" value="<?php echo $valorBaixa;?>">
                          <input type="hidden" name="totalBaixas" value="<?php echo $totalBaixas; ?>">                                        
                        </div>
                  </div> 

Where the first field reports me a database value and the 2 replicate that same value but the first field is blocking cannot change and the second I can change, Then I wanted to know how I do for when I change this second the same does not pass the value of varialvel.

inserir a descrição da imagem aqui

  • Let’s understand you have two fields You have 2 fields. the value of both comes from the database. One you want locked. the other you want to edit. Without changing the value of the previous one. That’s it ?

  • You hit in parts, the value of both are equal and comes from the bank but one already the first and locked and the second can edit only that the second value not to be typed can not pass the value of the first.

2 answers

1


A suggestion is to compare the values when changing the second input with keyup. If the entered value is greater than the first input, the code applies in the field the value of the first.

To do this you need to leave the values in the correct format, where you should only have one point separating the decimals (ex., 25066.77). For this I use 2 Replaces, the first removing the points of thousands (if any) and the second replacing the comma by dot. It is also necessary to use .parseFloat() to convert to float number (with decimals).

In the example below I put a mask to format the monetary value in the field:

// máscara para exemplificar
$("#valorBaixa").maskMoney({
   allowNegative: true,
   thousands: '.',
   decimal: ','
});

$("#valorBaixa").on("keyup", function(){
   
   // número mascarado
   var entr_valorMoeda = $("#entr_valorMoeda").val();
   // número formatado para comparação
   var entr_valorMoeda_num = parseFloat(entr_valorMoeda.replace(/\./g, "").replace(",", "."));
   // número mascarado
   var valorBaixa = $(this).val();
   // número formatado para comparação
   var valorBaixa_num = parseFloat(valorBaixa.replace(/\./g, "").replace(",", "."));
   if(entr_valorMoeda_num < valorBaixa_num){
      // se o primeiro valor for menor que o segundo
      // forço o segundo a ser igual ao primeiro
      $("#valorBaixa").val(entr_valorMoeda);
   }
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<div class="col-md-4">
   <label for="descricao">Valor título </label>
   <div class="input-group">
      <span class="input-group-addon">R$</span>
      <input type="text" id="entr_valorMoeda" class="form-control valor" value="25.066,67" readonly >
      <input type="hidden" class="valor"  name="entr_valorMoeda" value="<?php echo $conta->ENTR_ValorMoeda;?>">                                        
   </div>
</div>

<div class="col-md-4">
   <label for="descricao">Valor baixa </label>
   <div class="input-group">
      <span class="input-group-addon">R$</span>
      <input type="text" class="form-control valor" id="valorBaixa" name="valorBaixa" value="25.066,67">
      <input type="hidden" name="totalBaixas" value="<?php echo $totalBaixas; ?>">                                        
   </div>
</div>

  • Flw man was that same got show, I liked this your suggestion to compare the two.

  • Man me a doubt, if agr have a 3 field where now the value of 2 can not be greater than the (1 - 3) has as ?

  • Boy, then you have to ask a new question.

  • Ta new kkk flw

0

you do not want to pass the value of the variable when registered in the bank? or when the user will change inside the input? You can do this within an if.

if($variavel >= $variavel2){

echo "Não deixo cadastrar no banco"

}else{echo "ela é maior, então não pode alterar";}

Logic is more or less this.

  • And more or less this but it is not to appear message or anything and only limit the value of the same field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.