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

Question

Question

compare 3 fields

asked on March 18, 2024

Hello.  I borrowed a nice script for comparing (2) fields from @Michelle Suryadi and attempted to add the functionality of a 3rd field for comparison.  Of course it didn't work, thus why I am composing this request for help.

 Field 2 is filled by a data lookup.  Fields 3 and 4 are filled by scans done by the user.

Finally Field 5 is an HTML message we Hide or unhide (if either field 3 or 4 do NOT match field 2).

I really, tried not to post as I don't want to waste anyone's time, however, I do not really know what I am doing and I am hopeful the answer is quick for whoever volunteers to assist.

 

$(document).ready(function(){
$("#q5").hide();
$("#q3 input, #q4 input").change(function(){
    if ($("#q2 input").val() != $("#q3 input").val()) {
      $("#q5").show();
    }
    else if {
     ($("#q2 input").val() != $("#q4 input").val()) {
      $("#q5").show();
    }
    else {
      $("#q5").hide();
    }
});
});

 

0 0

Answer

SELECTED ANSWER
replied on March 19, 2024 Show version history

If you want it to only display the warning when both #q3 and #q4 have a value in it, you could do something like this:  

$(document).ready(function(){
  $("#q5").hide();
  $("#q2 input, #q3 input, #q4 input").change(function(){
    if ($("#q2 input").val() != $("#q3 input").val() && $("#q3 input").val() != '') {
      $("#q5").show();
    }
    else if ($("#q2 input").val() != $("#q4 input").val() && $("#q4 input").val() != '') {
      $("#q5").show();
    }
    else {
      $("#q5").hide();
    }
  });
});

 

This is factoring in @████████' suggestion to also trigger when #q2 is changed - then trying to include validation to ensure the #q3 or #q4 fields are populated before checking that they don't match.

It is saying to compare #q2 to #q3 as long as #q3 isn't blank - or to compare #q2 to #q4 as long as #q4 isn't blank. 

Is that what you are trying to achieve?

2 0

Replies

replied on March 18, 2024 Show version history

I didn't test your code, but I can see you have too many curly brackets opening your else if statement - you have one on line 7 and on line 8 - the one on line 7 shouldn't be there.

Updated code without it: 

$(document).ready(function(){
$("#q5").hide();
$("#q3 input, #q4 input").change(function(){
    if ($("#q2 input").val() != $("#q3 input").val()) {
      $("#q5").show();
    }
    else if 
     ($("#q2 input").val() != $("#q4 input").val()) {
      $("#q5").show();
    }
    else {
      $("#q5").hide();
    }
});
});

 

P.S. - you can use the {...} code button here in LFAnswers to make your code more readable.  Thanks!

2 0
replied on March 18, 2024

Related, I highly recommend copying any Forms JavaScript code you're trying to troubleshoot into Visual Studio Code or similar editor that has JavaScript syntax error detection. It will flag things like extra/missing brackets/quotes/semicolons/etc. right away.

3 0
replied on March 18, 2024

Only thing I would add is q2 to the .change event so if the lookup ever changes, the validation happens again. 
Could also add blank to the validation but that depends on the business case

3 0
replied on March 19, 2024

Hi Zach.  Can this be adjusted to wait until #q4 has a value entered before checking?  Currently, when I tab out of #q3, it shows #q5.  Then if I add a value for #q4 that also matches #q2, #q5 hides.

I would like #q5 to remain hidden until either, #q3 does not match because #q3 is null, #q4 does not match because #q4 is null.

0 0
SELECTED ANSWER
replied on March 19, 2024 Show version history

If you want it to only display the warning when both #q3 and #q4 have a value in it, you could do something like this:  

$(document).ready(function(){
  $("#q5").hide();
  $("#q2 input, #q3 input, #q4 input").change(function(){
    if ($("#q2 input").val() != $("#q3 input").val() && $("#q3 input").val() != '') {
      $("#q5").show();
    }
    else if ($("#q2 input").val() != $("#q4 input").val() && $("#q4 input").val() != '') {
      $("#q5").show();
    }
    else {
      $("#q5").hide();
    }
  });
});

 

This is factoring in @████████' suggestion to also trigger when #q2 is changed - then trying to include validation to ensure the #q3 or #q4 fields are populated before checking that they don't match.

It is saying to compare #q2 to #q3 as long as #q3 isn't blank - or to compare #q2 to #q4 as long as #q4 isn't blank. 

Is that what you are trying to achieve?

2 0
replied on March 19, 2024

This is exactly what I needed.  Thank you soo much!

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

Sign in to reply to this post.