Skip to main content
Participant
October 20, 2017
Answered

Need help with javascript and if/then/else statements

  • October 20, 2017
  • 1 reply
  • 528 views

Hello everyone!   I have searched this forum for days and tried many things but cannot find a workable solution to my problem.  My javascripting skills are novice at best.

I have created a numerical free text box "NumberValue" and a dropdown menu "TextChoices" with 5 text choices (ie Text1, Text2, Text3, Text4, Text5).  IF the user selects Text1 AND the previous numerical box "NumberValue" is >12 THEN make the "NumberValue" = 12.  IF the user selects Text1 AND the "NumberValue" is <12 THEN make "NumberValue" = the users input.  IF the user selects any other text choice THEN "NumberValue" = the users input.

Any advice other than closing the program and walking away?

This topic has been closed for replies.
Correct answer George_Johnson

You could use a custom Validate script for the NumberValue field, something like this:

// Custom Validate script of a numeric text field

(function () {

     // Get the value of the TextChoices field, as a string

     var sTextChoices = getField("TextChoices").valueAsString;

     // Alter this field's value if necessary

     if (+event.value > 12 && sTextChoices === "Text1") {

         event.value = 12;

     }

})();

1 reply

George_JohnsonCorrect answer
Inspiring
October 20, 2017

You could use a custom Validate script for the NumberValue field, something like this:

// Custom Validate script of a numeric text field

(function () {

     // Get the value of the TextChoices field, as a string

     var sTextChoices = getField("TextChoices").valueAsString;

     // Alter this field's value if necessary

     if (+event.value > 12 && sTextChoices === "Text1") {

         event.value = 12;

     }

})();

davidt36293775
Participant
October 20, 2017

George,

YOU, sir, are amazing!  Thank you!  Your script opens up so many options.  One followup question though.  I've noticed upon testing that if I were to populate the numeric text field first THEN select the proper text choice from the dropdown, the numeric text field still maintains the user entered value.  The script works great if the text choice was selected first then the numeric text field was populated.

Is there an option to have the numeric field update if the appropriate dropdown text choice is chosen after the numeric text field population?