Validate field with integer value, in decimal format (check if value is less than)

Asked

Viewed 686 times

3

Hello, my question is about a field validation for decymal value. If the value is less than 500,00 I show an error message, otherwise I pass to the second field, so the person type the value already adding the mask.

I’m using it this way:

function valid_simulation(form1) {

if(parseInt($("#selector").val()) < 500) {
alert("valor não é valido");
    return false;
}

}

This way I can validate a value less than 500,00, but if it is more than 1,000,00 it shows the error message.

  • your field has mask? if yes, the problem may be the .

  • Welcome to read this post https://answall.com/help/mcve

  • Putting parseFloat in place of parseInt no longer solves?

  • Laércio, won’t do it, thanks.

  • rLinhares, yes has mask... if I take the point ai validates, but I need the point. I have to find a way to add the point and validate. Thanks.

1 answer

4


The error is happening because in javascript language the decimals are separated by . and not by ,. So, if you "parse" a string that contains a point for int, the number becomes a different integer, in which case it ignores the comma:

parseInt("1.000,00"); //mostra 1
parseInt("10.000,00"); //mostra 10
parseInt("100.000,00"); //mostra 100

You need to remove the points to perform parseint correctly.

parseInt("1.000,00".replace(".","")); // mostra 1000
parseInt("10.000,00".replace(".","")); // mostra 10000
parseInt("100.000,00".replace(".","")); // mostra 100000

In your code:

function valid_simulation(form1) {

    if(parseInt($("#selector").val().toString().replace(".", "")) < 500) {
        alert("valor não é valido");
        return false;
    }

}
  • Biio, perfect worked. Thank you!

Browser other questions tagged

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