Javascript Mask Input with negative decimal

1

I have taken this code here from the forum, but I have doubts about using it with negative numbers.

The user needs to enter 1 ° the negative sign and then the numbers. Some results will be negative others, so I needed a way for the user to type the signal first and then the numbers.

Follow the code below:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.13.4/jquery.mask.min.js"></script><scriptlanguage="javascript" type="text/javascript">

$('.numerico').mask('#.##0', {
     reverse: true,
     translation: {
        '#': {
            pattern: /-|\d/,
            recursive: true
        }
     },
     onChange: function(value, e) {
        e.target.value = value.replace(/(?!^)-/g, '').replace(/^,/, '').replace(/^-,/, '-');
     }
});

</script>
    
asked by anonymous 13.06.2018 / 13:32

1 answer

0

In the mask you need to have the ID of the signal as optional and this is identified with the letter N in the translation setting, and thus you have the option of typing numbers with the negative sign < example :

$('.numerico').mask('N#.##0', {
    translation: {
        'N': {
            pattern: /[-]/,
            optional: true
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.13.4/jquery.mask.min.js"></script>

<input type="text" class="numerico" />
    
13.06.2018 / 14:32