How to check form values before sending it Javascript / jQuery

Asked

Viewed 41 times

0

I need to check two fields of a form before sending it.

I need to check that the 'stock_minimum' field is smaller than the 'stock_maximum' field. I have made numerous attempts and none worked well, incredible as it seems sometimes works and others do not (sometimes the conditions are simply ignored).

This is my code:

In the tag FORM joining with the BUTTON is like this (I will not put every form because it is great, and my interest is only in the 2 fields that I will insert)

function validaFormulario(){
	var estoque_minimo;
	var estoque_maximo;

	estoque_minimo = $('#estoque_minimo').val();
	estoque_maximo = $('#estoque_maximo').val();

	if(estoque_minimo >= estoque_maximo){
		alert('voce está fazendo isso errado...');
	}
	else{
		$('#formCadastroProdutos').submit();
	}                
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formCadastroProdutos" method="POST" action="cadastra_produto.php">
  <div class="form-group col-sm-4">
      <label for="estoque_minimo">Estoque minimo</label>
      <input type="number" class="form-control" id="estoque_minimo" name="estoque_minimo">
  </div>

  <div class="form-group col-sm-4">
      <label for="estoque_maximo">Estoque máximo</label>
      <input type="number" class="form-control" id="estoque_maximo" name="estoque_maximo">
  </div>

  <button onclick="validaFormulario()" id="btn_cadastra_produto" type="button" class="btn btn-primary">
      Salvar
      <span class="glyphicon glyphicon-floppy-disk"></span>
  </button>
</form>

I did it that way because I saw in a similar post around here... I am waiting for suggestions, I thank you!

2 answers

0


Just state the variáveis within the event of click, thus the values of inputs will be captured whenever there is the action and using the function Math.max(), we found the largest number between the two inputs, if the largest is the input estoque_minimo he enters the if If it’s not, he goes into else.

$('#btn_cadastra_produto').click(function(){
var estoque_minimo = $('#estoque_minimo').val();
var estoque_maximo = $('#estoque_maximo').val();

var max = Math.max(estoque_minimo, estoque_maximo);
if(max == estoque_minimo){
	alert('voce está fazendo isso errado...');
}
else {
//$('#formCadastroProdutos').submit();
    alert('ok,enviado')
}                
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formCadastroProdutos" method="POST" action="cadastra_produto.php">

        <div class="form-group col-sm-4">
            <label for="estoque_minimo">Estoque minimo</label>
            <input type="text" class="form-control" id="estoque_minimo" name="estoque_minimo">
        </div>
                            
        <div class="form-group col-sm-4">
            <label for="estoque_maximo">Estoque máximo</label>
            <input type="text" class="form-control" id="estoque_maximo" name="estoque_maximo">
        </div>

        <button 
        id="btn_cadastra_produto" type="button" class="btn btn-primary">
            Salvar
            <span class="glyphicon glyphicon-floppy-disk"></span>
        </button>

</form>

  • Jorge, first of all thank you for your reply. I tried as you explained, but it turns out that by persisting it ends up sending the form even if it does not meet the conditions. I tested so: I put 7 in minimum stock and 5 in maximum stock, I clicked on the button and he did not send the form, so far so good. However, if I edit the minimum stock to 10, or some larger number that contains 2 digits or more, it ignores the proposed condition. That’s the problem that happens with virtually every method I’ve tested so far, I can’t understand why the condition is ignored...

  • @Reginaldoboeke edited the answer, I used the Math.max() to capture the highest value, then just checked if the estoque_minimo was the greatest. Forehead there!

  • Hey Jorge. It worked fine, thank you very much! I didn’t know this Math.max(), I started studying web development a short time rsrs. Thank you very much, now I’m going to do some research to understand how Math.max() works. Hug!

  • @Reginaldoboeke for nothing! If the answer helped you, mark it as correct :) Hug!

0

Reginald,

you can add in your Form declaration the following code onsubmit="return validaFormulario()"

being as follows: <form id="formCadastroProdutos" method="POST" action="cadastra_produto.php" onsubmit="return validaFormulario()">

and modify your method to return Boolean if it is OK or not

function validaFormulario(){
var estoque_minimo;
var estoque_maximo;

estoque_minimo = $('#estoque_minimo').val();
estoque_maximo = $('#estoque_maximo').val();

if(estoque_minimo >= estoque_maximo){
    alert('voce está fazendo isso errado...');
    return false;
}
else{
    $('#formCadastroProdutos').submit();
    return true;
}                
}

This solution is more adopted than events onClick for being more secure, it is important to signal to the user where he has failed as well.

  • Hello Matheus, thank you for your reply. So, this way I would have to change my button to type="Ubmit" right? I did it that way and it didn’t work... Previously I had tried a solution similar to the one you explained, and it was the same of the above Jorge solution. It works in some cases, but when I put for example 5 in the minimum stock and 4 in the maximum, it does not send the form, so far so good. But if I edit the minimum stock by placing a number like 10 or higher (so it seems any number with 2 digits or more) it ignores the condition and submits the form :)

Browser other questions tagged

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