Validate input to receive value with separator per point with jquery?

0

I need you to only receive values with this formatting (20.00)

Resolved

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


<input class="form-control input-sm" onkeyup="somenteNumeros(this);" placeholder="valor" maxlength="5" type="text"  ng-model="numero.valor" />
    
asked by anonymous 30.12.2015 / 14:29

2 answers

1

The expression to validate this formatting is /\d*(\.\d\d)$/

    
30.12.2015 / 16:07
0

Try something like this:

function somenteNumeros() {
        var er = /\d*(\.\d\d)$/;
        var valCampo = document.getElementById('campo').value;
        if (!er.test(valCampo)) {
            document.getElementById('campo').value='';                   
        }
}

To allow only numbers, you can do something like this:

add a function similar to this:

function apenasNum(){
    var kc = event.keyCode;
    if(kc >= 48 && kc <= 57){
        return true;
    }else{
        return false;
    }
}

In the form field you add the onKeypress event by calling the function you created:

<input type="text"  onKeypress="return apenasNum();">
    
30.12.2015 / 17:34