Extract only the numbers from a Javascript text box

Posted on

Question :

How do I extract the numeric value of a text box in javascript, eg:

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

Thanks to anyone who helps me

    

Answer :

You can filter the value of the field:

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

    

You can use the regular expression to replace all non-numeric characters, this is represented by D , g at the end means that substitution is done on all found elements by default (without g ) only a replacement is done.

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

    

You can set the input with type number :

<input type="number" />

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

    

Leave a Reply

Your email address will not be published. Required fields are marked *