Multiplying Values in JAVASCRIPT

2

I wanted to get the value of the Field where it places the value of R $ and limit [tipo (1<= 99 ) *23 e (>100) *25 ] how?

Code I have:

<!DOCTYPE html>
<html>
<head>
       <script src="jquery.min.js" type="text/javascript"></script>
    <script src="jquery.maskMoney.js" type="text/javascript"></script>
    <style>
        * { margin: 0; padding: 0; font-size: 13px; font-family: Verdana;}
        #main { width: 1500px; height: 1800px; margin: auto;}
        section { margin-top: -385px; margin-left: 608px; }
        #demo3 { display: none;}
    </style>
</head>
<body>
<script> 
function soma() 
{
form.campo4.value = parseInt(form.campo1.value*1) * parseInt(form.campo2.value*1) 
}
</script>
<script type="text/javascript">
$(function(){
 $("#demo4").maskMoney({symbol:'R$ ', 
showSymbol:true, thousands:'', decimal:'.', symbolStay: false});
 })

 $(function() {
   $(document).on('click', 'input[value][id=demo4]', function() {
     this.select();
   });

 });
 $("#demo4").bind('input propertychange', function(){
    if($(this).val() > 4){
         $(this).val() = 4;
    }else if($(this).val() < 1){
         $(this).val() = 1;
    }
});

</script>
<script>
  function limitarInput(obj) {
    obj.value = obj.value.substring(0,8);
     }
</script>

<div id="main">
<section>
<form name="form">
<input name="campo1" id="demo4" value="00,00" onkeyup="limitarInput(this)" style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; text-align: center; color: darkolivegreen"><br> 

<input name="campo2" value="12" id="demo3"><br>  

    <input type="button" onclick="soma()" value="CALCULAR"
       style="width: 200px; height: 40px; background-color: transparent; font-size: 30px; margin-top: 26px; cursor: pointer; text-align: center; color: transparent; border: 1px solid black;">

<input name="campo4" readonly id="resultado" value=""  style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; margin-top: 0px; display: block; text-align: center; color: maroon"><br>
    </form>
</section>
</div>
</body>
</html>
    
asked by anonymous 03.09.2017 / 15:22

1 answer

3

Just compare the input value and impose condition

function soma() 
{
	var valor;
	var campo = form.campo1.value;
	if(campo >=1  && campo < 100){
		valor=23;
	}else{
		valor=25;
	}
	form.campo4.value = parseInt(campo) * parseInt(valor) 
}

  function limitarInput(obj) {
    obj.value = obj.value.substring(0,8);
  }
<div id="main">
<section>
<form name="form">
<input name="campo1" id="demo4" value="00,00" onkeyup="limitarInput(this)" style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; text-align: center; color: darkolivegreen"><br>  

    <input type="button" onclick="soma()" value="CALCULAR"
       style="width: 200px; height: 40px; background-color: transparent; font-size: 30px; margin-top: 26px; cursor: pointer; text-align: center; color: transparent; border: 1px solid black;">

<input name="campo4" readonly id="resultado" value=""  style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; margin-top: 0px; display: block; text-align: center; color: maroon"><br>
    </form>
</section>
</div>
  

If you want to use a decimal part, as noted by Anderson Carlos Woss's comment, which can make a substantial difference in the result, use parseFloat instead of parseInt

function soma() 
{
	var valor;
	var campo = form.campo1.value;
	if(campo >=1  && campo < 100){
		valor=23;
	}else{
		valor=25;
	}
	form.campo4.value = parseFloat(campo) * parseFloat(valor) 
}

  function limitarInput(obj) {
    obj.value = obj.value.substring(0,8);
  }
<div id="main">
<section>
<form name="form">
<input name="campo1" id="demo4" value="00,00" onkeyup="limitarInput(this)" style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; text-align: center; color: darkolivegreen"><br>  

    <input type="button" onclick="soma()" value="CALCULAR"
       style="width: 200px; height: 40px; background-color: transparent; font-size: 30px; margin-top: 26px; cursor: pointer; text-align: center; color: transparent; border: 1px solid black;">

<input name="campo4" readonly id="resultado" value=""  style="width: 200px; height: 50px; background-color: transparent; font-size: 30px; margin-top: 0px; display: block; text-align: center; color: maroon"><br>
    </form>
</section>
</div>
    
03.09.2017 / 16:39