Paste Numbers

1

How could I do to remove the dots from a number formats?

For example, I get and copy a value 000,000,000 and paste it into my input , it would automatically leave this formatting of it and it would be " 000000000 ".

I found a mask, but it is not running here.

My input:

<input type="text" class="form-control" name="var_est" value="" onchange="tiraMascara(this)" placeholder="" data-size="small" mask="#000.000.000.000" data-only-numbers="" required="required" style="width: 100%;">

The mask:

<script>
function removeMaskMoney (x){
    x = ""+x;
    y = 0;
    if ((x.replace(",", ".") != x )){
        if (x.replace(".", "") != x ){
            aux = x;
            x = x.replace(".", "");    
            x = x.replace(".", "");                            
        }
        else {
            aux = x;
        }
        if(x.replace(",", ".") != x){
            x = x.replace(",", ".")
        }else{
            x = aux;
        }
    }
    if (isNaN(parseFloat(x)) ){
        x = 0;
    }else{
        x = parseFloat(x)
    }
    return x;
}

function tiraMascara(e){
    value = removeMaskMoney ($(e).val());
    $("[name=var_est_sem_mask]").val( value)
}
</script>
    
asked by anonymous 18.09.2018 / 13:50

1 answer

3

You can use regular expression and String.replace() to replace any non-number character with '' .

"000.000.000".replace(/\D+/g, '');  // "000000000"

\D causes Regex matches any character other than a number ( [0-9] ).

+ is a quantifier that means "one or more times". That is, it will match any character other than a number that repeats 1 or more times.

Finally, the g flag specifies that replace will be global, ie it will not stop at the first substitution.

A very simple example that works:

var $input = $('#teste');
var $feedback = $('#feedback');

$('#btn').on('click', function() {
  $feedback.html($input.val().replace(/\D+/g, ''))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="teste"/>
<button id="btn">!!!</button>
<div id="feedback"></div>
    
18.09.2018 / 14:02