Extract only numbers from a Javascript text box

Asked

Viewed 249 times

3

How do I extract the numeric value from a javascript text box, e.g.:

valor imputado = 1a321q00
valor extraido e adicionado na variável = 132100

I thank those who help me

  • 2

    "text box" Is that an input or a textarea? You can show HMLT?

3 answers

3


You can filter the field value:

var valor_extraido = valor_extraido.replace(/[^0-9]/g,'');

3

You can use regular expression to replace all characters that are not numbers, this is represented by \D, the g in the end means that the substitution is done on all found elements by default (without the g) only one replacement is made.

"1a321q00".replace(/\D/g, "")

0

You can set the input with type number:

<input type="number" />

Detail is that the character and can be used as it means exponentiation.

Browser other questions tagged

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