Validate input to receive value with point separator with jquery?

Asked

Viewed 188 times

0

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

Solved

    <!-- 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" />

2 answers

1

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

  • worked out, accepted with the value with virtual, plus accept letters too, have to avoid it? I appreciate your help

  • More dai is your function logic. According to the function you passed if the value passes in the test then you clear the field. It’s like this, it’s not?

  • It is validating correctly, the problem is that give to write text too.

  • solved! thanks, posted the code in the question

0

Try something like:

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();">
  • It is validating correctly, the problem is that give to write text too.

  • See in reply, I edited and added another option to block letters.

  • solved! thanks, posted the code in the question

Browser other questions tagged

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