Problems with jquery and input mask

Asked

Viewed 361 times

0

I’m trying to put a value on the inside of a input which is masked both in typing and in the entered value.

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

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

<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 this. Below the code of the Mask:

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

Is inside a ready quiet, quiet, quiet here.

Again, my problem is:

  • I cannot fix the input value with the saved input value.

Grateful to all who can help!!!

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

100.000,00 => 100,00
10.000,00 => 10,00
1.000.000,00 => 1,00

They saw that he was always taking the digits before the first point and adding the ",00" [End EDIT 1]

  • Try to change Mask and use it like this: $('. currency'). Mask("#. #0,00", {Reverse: true});

  • I’m sorry bro, but the problem persists... The mask in the typing is fine, but when I save it changes the value, as I said above, I always test using 100.000,00 and when giving the "save" the value changes to 100,00 and every time I increase a zero in the typing, it decreases 1 zero in the number that gets printed.

2 answers

1


Since the input is text type, and has commas, what the input is returning is a string, and for that, you will have to format the value of it turning 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 Let value there, in case, you will get the value of the input. (I think you now know how to do rs) there with the number formatted this way, can you do the calculations you need.

  • Friend, I am trying to adapt your suggestive codes to my need, and I still need the output value to be with '.' and ',' still in the mask, ok?

  • 1

    Yes, @Lougansdematos, but to do the calculation you first have to take the '.' and ','. After the calculation done, you can put the same again in the result that was returned.

  • I get it, and I’ve tried, bro, but it still hasn’t worked... What is interesting is that in some combinations I made it worked the first time, but from the second time updating some other data, the value breaks and changes... The Mayor thought it had worked but unfortunately has not yet given rsrs... I keep searching ^^

  • 1

    The table that receives this value in DB is int?

  • is as decimal (19,2)

  • 1

    It will not work then, because the la value of your input, as it has comma, etc. It is a string, it would be right to save the same as varchar... Or, treat the same as I suggested, in the above function.

  • 1

    Aaah bro... we try to do right né kkkkk but if it doesn’t work! Thanks man! You helped me... tmj and I thank you for taking the time to give me a help :D

Show 2 more comments

-2

Already tried using the function mask of Jquery:

Behold:

<script>
    $(document).ready(function () { 
        $(".real").mask('#.##0,00', { reverse: true });
    });
</script>
  • As I mentioned in the comment over there on Lucas Carvalho, it didn’t work... :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.