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!
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...
– Reginaldo Boeke
@Reginaldoboeke edited the answer, I used the
Math.max()
to capture the highest value, then just checked if theestoque_minimo
was the greatest. Forehead there!– Jorge.M
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!
– Reginaldo Boeke
@Reginaldoboeke for nothing! If the answer helped you, mark it as correct :) Hug!
– Jorge.M