Obtain only numbers in an input that receives XX ml (example: 200ml)

Asked

Viewed 215 times

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?

  • 1

    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

  • 1

    parseInt(nr, 10)

  • @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

  • 1

    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).

  • 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...

  • the problem actually was the comma on the spot on the real number.... thanks for the friendly help! always good to learn

  • 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.

Show 2 more comments

1 answer

4


If this string has values first and letters/units then you can use the parseInt that ignores what comes after the numbers and gives as return a number, ie guy: number.

var tamanho = parseInt($("#tamanho").val(), 10);

If you need decimal part values, then it gets a little more specific and you can do so:

var string = $("#tamanho").val().replace(/\./g, '').replace(',', '.');
var tamanho = parseFloat(string.match(/[\d\,\.]+/)[0]);

The parseFloat turns a string into a number with decimal part, but is more limited than the parseInt, you need to have the string only with digits and using the dot to indicate the decimal part and not the comma.

Examples:

var tamanhoSimples = '200 ml';
console.log(parseInt(tamanhoSimples, 10));

var tamanhoComParteDecimal = '5.200.544,46 ml';
var stringLimpa = tamanhoComParteDecimal.replace(/\./g, '').replace(',', '.');
console.log(parseFloat(stringLimpa.match(/[\d\,\.]+/)[0]));

Browser other questions tagged

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