Problems validating balance

Asked

Viewed 44 times

1

Hello, my personal beauty?

I have a problem in my code, I am doing a validation to check if the amount entered by the user is greater than the balance, if it is I send an error to the user.

The code is as follows: :

                    $('#txb' + sequencia).on('change', function () {

                    var saldo = $($('#tbPimItem tbody tr td').get(7)).text();
                    var qtde = $(this).val();

                    console.log(qtde > saldo);

                    if (qtde > saldo) {

                        var mensagem = {
                            Tipo: 'A',
                            Mensagem: 'Atenção, a quantidade não pode ser maior que o saldo.'
                        };
                        console.log();

                        $(this).val(saldo);
                        exibirMensagem(mensagem, 100);
                    }

                });

He’s inside that code:

$(result.Retorno.PimItem).each(function () {

                sequencia++;

                $('#tbPimItem tbody').append($('<tr style="cursor:pointer"></tr >')

                    .append($('<td><input type="checkbox" id="cb' + sequencia +'" /></td>'))
                    .append($('<td></td>').text(this.Sequencia))
                    .append($('<td></td>').text(this.Item.Id))
                    .append($('<td style="width:80px;"><input type="number" max="3" id="txb' + sequencia + '" class="form-control" style="width:80px;" /></td>'))                    
                    .append($('<td></td>').text(this.Item.Descricao))
                    .append($('<td></td>').text(this.DescricaoComplementar))
                    .append($('<td></td>').text(this.Ordem.Id))                      
                    .append($('<td></td>').text(this.Item.Saldo))   
                );  

                $('#txb' + sequencia).val(this.Item.Saldo);

                $('#txb' + sequencia).on('change', function () {

                    var saldo = $($('#tbPimItem tbody tr td').get(7)).text();
                    var qtde = $(this).val();

                    console.log(qtde > saldo);

                    if (qtde > saldo) {

                        var mensagem = {
                            Tipo: 'A',
                            Mensagem: 'Atenção, a quantidade não pode ser maior que o saldo.'
                        };
                        console.log();

                        $(this).val(saldo);
                        exibirMensagem(mensagem, 100);
                    }

                });

                if (this.Item.Saldo !== 0) return;
                $('#' + sequencia).prop('disabled', true); 
                $('#cb' + sequencia).prop('disabled', true);

            });

My problem is :

The balance is equal to 3, when I type 4 it validates, when I type 10 it does not validate. If you type 30 it validates.

it’s as if he checks out only the first digit.

2 answers

1

The comparison is being made between strings, you can cast to Number:

var saldo = parseFloat($($('#tbPimItem tbody tr td').get(7)).text());
var qtde = parseFloat($(this).val());

1


You need to use numbers in comparison and not text (strings). A suggestion would be:

var saldo = $('#tbPimItem tbody tr td::last-of-type').text();
var qtde = this.value;

console.log(Number(qtde) > Number(saldo));

Browser other questions tagged

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