I need a mask that always places the negative sign in front of the value typed [closed]

1

I need it to look like this: -20.00

    
asked by anonymous 16.04.2018 / 22:06

2 answers

0

Just concatenate the - (minus) sign to the returned value

$('input').change(function(){
 var input = "-" + ($('#numInput').val());

 console.log(input);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="numInput">

To use in mathematical calculations requires turning the comma into a point

$('input').keyup(function(){
var input = "-" + $($(this)).val().replace(',','.');

 var result = ((100+parseFloat(input)).toFixed(2));

 console.log(result);

 var resultVirgula = result.replace('.',',');

 console.log(resultVirgula);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="numInput">
    
16.04.2018 / 22:33
1

A very simple implementation for this problem:

$('#mask').keyup(function(){
  if((this.value.search('-') == -1 && this.value.length > 0 )){
    $(this).val('-' + this.value);
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text"  id="mask" >Máscara
    
16.04.2018 / 22:37