It turns out that I am trying to validate that one input is not greater than another but it does not work correctly
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="application/javascript">
function enviar() {
let totalAnopheles = $('#totalAnopheles').val()
let totalZancudos = $('#totalZancudos').val()
if (totalAnopheles > totalZancudos) {
console.log("error")
} else {
console.log('correcto')
}
}
</script>
<div class="col-md-4">
<div class="form-group has-feedback">
<label for="totalZancudos">Total Zancudos:</label>
<input type="number" class="form-control allownumericwithoutdecimal" id="totalZancudos" name="totalZancudos" required="required">
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group has-feedback">
<label for="totalAnopheles">Total Anopheles:</label>
<input type="number" class="form-control allownumericwithoutdecimal" id="totalAnopheles" name="totalAnopheles" required="required">
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
</div>
</div>
<button onclick="enviar()">click</button>
the idea is that wl second input (Anopheles) cannot be greater than Zancudos
You are not capturing the values of the
input
. You can useval()
for it, since you are using jQuery.On the other hand, if you use
let
the data, it will always keep the first value that it hadinput
when you called the function. It would be convenient to usevar
, because this way the data will be evaluated with the value that it has at that moment.A small detail: when obtaining with
.val()
the values of each field, you are obtaining each value as a string (although you use<input type="number">
). Two ways to convert the value you got to a Number are:+
(as I leave it in the code),parseInt()
leaving as final result: