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

Question

Question

java to validate one field greater than or equal to another field.

asked on September 4, 2015

I have a text field(Quotes Provide) that I want to validate against a drop down field(number of Quotes Required) to be greater than or equal to and show submit if its true.

 

Current Code not validating but it does show the submit button when populated:

 

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

 

0 0

Answer

APPROVED ANSWER
replied on September 4, 2015 Show version history

There is a syntax error in your JavaScript. Use

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

You can follow Carl's recommendation about validating the value as a number first. Also, q18 is a select (since it's a dropdown) and not an input.

0 0

Replies

replied on September 4, 2015 Show version history

Hi Tommy,

 

It looks like you are comparing two number values. Text input is generally treated as a string so if you wish to compare the string "5" to the string "6" you will want to change it to an integer first.

http://stackoverflow.com/questions/7565574/jquery-number-compare

 

There is also a syntax issue with '#q18 input' as it looks like the code above is comparing the string value of q22 input field value to the literal representation #q18 input instead of what you are looking for, which is the actual value.

 

Try using:

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

 

0 0
replied on September 4, 2015

Doh, good catch. I missed the dropdown part.

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

Sign in to reply to this post.