Recovers input value and compare with jquery value

Asked

Viewed 106 times

1

I have this code that retrieves the value entered in the input and compares it with a value x, in this case 25.00. The problem is that when I change the field inside the form it shows the error saying that the entered value is less than 25, but if the person does not correct and submit the form he sends anyway, i need that if the entered value is less than 25 it shows the error message and when the user submits the form it returns to show the error not letting the form follow and when the user enters the correct value, ie 25 or higher, the error message disappears.

<div class="donate_amount_field m-0">
     <input type="text" name="amount" class="form-control" value="" required>
 </div>                                
 <div class="alert" id="error_amount" style="display: none;">valor menor que 25</div>


 $('input[name=amount]').blur(function() {

    if ( $(this).val() < "25,00" )

       showError_amount();

  });

  function showError_amount() {

     $("#error_amount").show();

  }

1 answer

0


$('input[name=amount]').focusout(function() {
    var valorDigitado = parseFloat($(this).val());
    if (valorDigitado < 25) {
       $('input[name="amount"]').val(25.00);
       showError_amount();
       } else {
        $("#error_amount").hide();
       }
       
  });

  function showError_amount() {
     $("#error_amount").show();
  }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<div class="donate_amount_field m-0">
     <input type="text" name="amount" class="form-control" value="" placeholder="Valor Maior que 25.00" required>
 </div>                                
 <div class="alert alert-danger mt-3" role="alert" id="error_amount" style="display: none;"><strong>Atenção!</strong> Valor menor que 25!</div>
 </form>

See if this helps! As a quick solution, I simply assign "automatically" the value 25 within the input, if the value entered by the user is less than 25, understood?

I hope I’ve helped you! Any doubt is just talk ;)

  • 1

    His help was of great value, but it lacked a detail that I forgot to mention, the field in question he has a mask that formats the value in Brazilian real 0,00 dai when I type a value even with the mask it shows the error message saying that the value is lower and puts 25 in the field and that’s where the problem is, it puts the value 25 but is not in the format 25,00, so if a user does not correct it will command in the format that the code puts which is 25 and so will give error in the request is what is missing this value be in the format 25,00, have to leave so, I tried to change here but did not give.

  • I got it. I’ll tell you what. Copy here all html and js that you have (including this issue of the mask), because it is better for us to understand and be able to help you in the best way, quiet?

  • 1

    André thank you so much, for the help, I’ve already solved, just put this part of the code $('input[name="amount"]'). val(25.00); where this the 25.00 changed as string and put between "25.00", so it worked perfectly. Thanks again.

  • Show brother. I’m glad you succeeded! Hugging.

Browser other questions tagged

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