Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Javascript for relating two textfields

Avatar

Level 2

Have a form with 2 text fields, txt1 and txt2. The user has to enter a code between 01 and 99 into txt1. If the entered code in txt1 is between 71 and 84, txt2 must be completed by entering a code between 01 and 73 or 85 to 99.

Wanted, that when the code was entered into txt1, if it was 71 to 84 that txt2 fill colour would change to yellow, highlighting that a code entry is required. When the user entered the code the fill colour would disappear. Also, if they wrongly entered a value in to txt2 of 71 to 84, the fill colour would change to red or a an alert message. If a correct code range value was then entered into txt2 the fill colour would disappear.  

Can anybody help me?

1 Accepted Solution

Avatar

Correct answer by
Level 7

In the exit event of txt1 put:

if (this.rawValue >= 71 && this.rawValue <=84) {

    txt2.fillColor = "225,225,0";

    }

else {txt2.fillColor = "255,255,255";}

In the exit event of txt2 put:

if (this.rawValue >= 71 && this.rawValue <= 84) {

    this.fillColor = "255,0,0";

    }

else if (this.isNull) {

    this.fillColor = "255,255,0";

    }

else {this.fillColor = "255,255,255";

}

View solution in original post

4 Replies

Avatar

Level 8

On the exit event of txt1 you can put this JavaScript:

if (this.rawValue>=71 && this.rawValue<=84)

txt2.access="open";

else

txt2.access="protected";

If you have the Highlight Existing Fields enabled, it will go blue indicating that it is editable.

For the warning on txt2 you can have this JavaScript on the exit event:

if (this.rawValue>=71 && this.rawValue<=84)

xfa.host.messageBox("warning message here");

Kyle

Avatar

Level 2

Thank you. Can you please help me as my knowledge of JavaScript is shallow. The script I was playing with that was not working was along the lines of:

if (txt1.rawValue <= 84 && txt1.rawValue >= 71) {

txt2.fillColor = "255,255,0";

}else if (txt2.rawValue  <= 84 && txt1.rawValue >= 71) {

txt2.fillColor = "255,0,0";

}else if (txt1.rawValue <= 70 && txt1.rawValue >= 85) {

txt2.fillColor = "255,255,255";

}

My question relates to fillcolor. Is it better not to try and fillcolor textfields? I had tried a switch statement, but again with no success.

Avatar

Correct answer by
Level 7

In the exit event of txt1 put:

if (this.rawValue >= 71 && this.rawValue <=84) {

    txt2.fillColor = "225,225,0";

    }

else {txt2.fillColor = "255,255,255";}

In the exit event of txt2 put:

if (this.rawValue >= 71 && this.rawValue <= 84) {

    this.fillColor = "255,0,0";

    }

else if (this.isNull) {

    this.fillColor = "255,255,0";

    }

else {this.fillColor = "255,255,255";

}

Avatar

Level 2

Thank you. Looks that simple when you see script. Spent hours dancing round it without success.