You are viewing limited content. For full access, please sign in.

Question

Question

currency javascript greater than

asked on May 30, 2022

Hi,

 

I have been trying to add some validation to a currency field, so that values entered must be less than the value in another currency field.

I've borrowed some code from this forum and replaced the fields with my own:
 

$(document).ready(function() {
  $('.Submit').hide();
  $('#q15 input').on('blur change', function() {
    if ($('#q15 input').val() >= $('#q5 input').val()) {
      $('.Submit').hide();
    } else {
      $('.Submit').show();
    }
  });
}); 

#q5 is my max/upper limit field.

#q15 is my currency field that I want the "validation" to apply to. 

The end result should be that if a value is entered in #q15 that is greater than the value in #q5, then the Submit botton should be hidden.

If it is less than or equal to #q5 then the submit button is visible.

I'm getting odd results whereby if I enter an exact amount it works, but a "less than" amount does not match.

Any suggestions? Thank you!

0 0

Answer

SELECTED ANSWER
replied on May 31, 2022 Show version history

You'll need to convert the two values to numbers.  As it is, Javascript will treat them as strings, not numbers, which means you are doing string comparison, which is basically comparing the values alphabetically.

Alphabetic comparison with numbers will result in weird evaluations such as: 150.00 < 20.00 = TRUE because 1 < 2.  We need it to do numerical comparison.

I haven't tested this, but I think this will work - here's your code modified to convert the values to floats before evaluating them: 

$(document).ready(function() {
  $('.Submit').hide();
  $('#q15 input').on('blur change', function() {
    if (parseFloat($('#q15 input').val()) >= parseFloat($('#q5 input').val())) {
      $('.Submit').hide();
    } else {
      $('.Submit').show();
    }
  });
}); 

 

0 0
replied on May 31, 2022

That did the trick! Thank you!

1 0
replied on May 31, 2022

Fantastic!

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.