Validate input to only receive numbers with jquery?

3

I need this input to only receive numbers:

<input class="form-control input-sm " placeholder="número" maxlength="4"  type="text"  ng-model="numero.nJogo" />

This I need to get only values with this formatting (20.00)

<input class="form-control input-sm" placeholder="valor" maxlength="5" type="text"  ng-model="numero.valor" />
    
asked by anonymous 30.12.2015 / 03:42

2 answers

9

This script only accepts numbers and . :

 <script>
    function somenteNumeros(num) {
        var er = /[^0-9.]/;
        er.lastIndex = 0;
        var campo = num;
        if (er.test(campo.value)) {
          campo.value = "";
        }
    }
 </script>

Just add the onkeyup event to your input, like this:

<input class="form-control input-sm" onkeyup="somenteNumeros(this);" maxlength="5" type="text"  ng-model="numero.valor" />
    
30.12.2015 / 10:15
-3
$(input).keydown((event): boolean => {
   return ('0123456789'.search(event.key) > 0);
});
    
15.09.2016 / 18:08