Hello,
I have an input called spent, and the value you entered can not be greater than 1,000,00 , if greater than 1,000,00 a modal appears on the screen.
Could you help me with how to do this javascript, please?
Hello,
I have an input called spent, and the value you entered can not be greater than 1,000,00 , if greater than 1,000,00 a modal appears on the screen.
Could you help me with how to do this javascript, please?
in this
if($('#gasto').val() >= 1000.00){
leave the value as numericIf the entry in the input is comma, you have to change the comma per point to make a comparison
$(function() {
$(".btn-gasto").click(function() {
if($('#gasto').val() >= 1000.00){
//alert('Para o seu consumo iremos tratar o seu orçamento e projeto de forma exclusiva. ');
$("#myModal").modal('show');
event.preventDefault();
} else{
alert('menor que 1000.');
}
});
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><formname="calculadora" method='post' action='https://www.sunkit.com.br/resultado/'>
<input type="tel" id="gasto" name="gasto" placeholder="R$ 0.00" required>
<button type="submit" name="submit" class="btn-gasto">Calcular</button>
</form>
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Consumo acima de R$1000.00 </h4>
</div>
<div class="modal-body">
<p>Para o seu consumo iremos tratar o seu orçamento e projeto de forma exclusiva.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
The
preventDefault
method, which as the name already gives an idea, prevents the default behavior of the object, ie cancels the behavior that the elements usually have on the page, then in this case, prevents the button click from submitting the form
You can use addEventListener()
, from there you can get the value and do the verification:
INPUT_OBJECT.addEventListener('input', function (evento) {
alert(this.value);
});
Any questions are available!
Source: Pure JavaScript listen to input
There is a simple and easy solution to your problem, using only html5 .
<!DOCTYPE html>
<html>
<body>
<form>
<input type="number" required="true" name="price" min="0" max="1000.00" step=".01">
</form>
</body>
</html>
Explanation of tags
If you want your input to have negative values, simply remove the min attribute.
I hope to have helped and welcome to stackoverflow!
Complementing the above answer:
INPUT_OBJECT.addEventListener('input', function (evento) {
evento.preventDefault();
if(parseFloat(this.value) > 1000.00){
alert('O número é maior que 1000.00');
}
});
You could call this function with onClick if you want to put it on a button.