Problems with jquery masking and input

0

I'm trying to put a value inside a input that is masked both in typing and in the value entered.

Well, I have a list of real estate listings in the database where I pull the front, okay?

And not going into many specifications, whenever I enter a value in INPUT (Ex: 100.000,00 ) it comes quietly formatted, however when I click "Save" (from my system, irrelevant now) the value of input changes to 100.00 . Here is the code for the front:

<div class="col-md-4">
<div class="form-group">
    <label class="bold label-color-cinza">Preço</label>
    <input type="text" maxlength="" class="form-control input-sm moeda" name="valor" value="<?= $imovel->valor ?>" placeholder="Preço">
</div>

Please, I'm trying everything and it always comes to that. Below the mask code:

$('.moeda').mask('999.999.999,99');

You are inside a ready calm, here is quiet.

Again, my problem is:

  • I can not set the input value to the value written to the input.

Thanks to all who can help !!!

EDIT 1: After some suggestions from you the problem persisted. The typing mask is fine. I always test using the 100,000.00 value and the printed result gets 100.00.

100,000.00 = > 100,00
10,000.00 = > 10,00
1,000,000.00 = > 1.00

You have seen that he is always picking up the digits before the 1st point and adding the ", 00" [End EDIT 1]

    
asked by anonymous 02.07.2018 / 15:55

2 answers

1

As the input is type text, and has commas, what the input is returning is a string, and for that, you have to format the value of it by transforming it into a number:

function formatVal(val){
  let valor = val;
  let valTot = 0;
  return valTot += Number(valor.replace('.', '').replace(',', '.'));
}

console.log(formatVal('100.000,97'))

In the let value there, in case, you will get the value of the input. (I think now you know how to do rs) there with the number formatted that way, you can do the calculations you need.

    
02.07.2018 / 18:38
-2

You have already tried to use the Jquery mask function:

See:

<script>
    $(document).ready(function () { 
        $(".real").mask('#.##0,00', { reverse: true });
    });
</script>
    
02.07.2018 / 16:49