5
I am using the Jquery mask to automatically put the ml (milliliter) suffix in the input that gets the size of a bottle.
being the input id #size:
var tamanho = $("#tamanho").val();
An example 200 ml take only the value 200.
but the console.log(tamanho)
returns 200 ml
and I need only the value, if it is not impossible to make calculations.
Which regular expression should I use to capture only input numbers?
where I should put this regular expression to add the value to the variable tamanho
?
tamanho = parseInt( $("#tamanho").val() );
- in this case it is good that the numbers do not have zero left, so that no implementation understands as octal. See the @Sergio syntax below to specify the basis manually, as well as the MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt– Bacco
parseInt(nr, 10)
– Sergio
@stock
tamanho = parseInt( $("#tamanho").val() );
solved the problem now I’m getting the 200 only and need to make a split with him with a real number, but I’m getting NAN’s response– Bruno Cabral
That’s because of the R$ prefixing the value. You can even go around these things, asking questions and people answering, but it will always be a sort of gambit. Probably the ideal would be for you to take advantage of what you are developing, and separate what is currency, what is value and what is unity, not only by making your life easier, but mainly by being the most correct in the vast majority of situations. R$ is a fixed string, can go off the field. So I would suggest that you fix it before proceeding (of course, the application is yours, you should do as you see fit, I just gave the tip while it’s time).
– Bacco
So when I give a console log the value it only returns the number, and not the R$... for example the user puts R $ 3,50, 200 ml, I get 3,50 and 200 on the console, I thought that the division would be performed with these values...
– Bruno Cabral
the problem actually was the comma on the spot on the real number.... thanks for the friendly help! always good to learn
– Bruno Cabral
Yeah, the comma really needs to be treated, but it’s basically what the Elder already dealt with
.replace(/\./g, '').replace(',', '.');
, by removing all the stitches, and exchanging the comma for one stitch. the problem in this case is if the user uses a point in place of the comma, but understanding the logic you adapt easier.– Bacco