Change height and width commas with mascára

0

I have a code that calculates the amount of rolls required according to the width and height of the person's wall. Currently the code for the comma mask is this:

        $('.virgula').mask('####00,00', {reverse: true});

        $("#altura").focusout(function (){
            try{
                var altura = parseFloat( $("#altura").val().replace(',','.') );
                var largura = parseFloat( $("#largura").val().replace(',','.') );
                var divisor = 1.20;
                var resultado = largura / divisor;

That is, when the person types "1" and "5" the number appears as 15 meters and only after it enters the "0" is that it appears 1.50 meters.

I would like to know if there is a possibility of the number appearing as "1,5" with two digits, as if the mask was "## 0,0" and after two digits it returns to the mask "#### 00.00 ". I've tried some IFs and nothing.

    
asked by anonymous 29.10.2016 / 02:56

1 answer

0

You can change the onKeyUp mask by counting the characters that exist in the input:

$('.virgula').mask('#####0,0', {reverse: true});

$('.virgula').on('keyup', function() {
  if( $(this).val().length > 3 ) {
      mascara = '####00,00';
  } else {
    mascara = '####0,0';
  }
  
  $('.virgula').mask( mascara, { reverse: true});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.0/jquery.mask.js"></script>

<input type="text" name="valor" class="virgula" />
    
29.10.2016 / 03:46