Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Message box with required fields

Avatar

Level 3

I have a check box, and when I check it I would like for another fields to be required. I seem to have that part working fine, so that when the user tries to submit the form without entering data into that field, it throws an error message.

I would like to take it one step further, that when the user TABS out of the field, it also pops up a message. And when the check box is NOT checked, then there would be no error message if the field is blank.

I put the following code on the field under the exit event:

if (this.rawValue == '' || this.rawValue == null || trainingCB =="1") {

    xfa.host.messageBox("Training agenda required");

}

However, the message box is popping up even when the check box is NOT checked. How do I get this to pop up only if the field is blank AND the check box IS checked?

1 Accepted Solution

Avatar

Correct answer by
Level 3

Figured it out. Two simple problems.

First, I wasn't using the && for and in my if statement. Second, I forgot to put .rawValue on something. So the corrected code looks like this:

if (trainingCB.rawValue == "1" && (this.rawValue == "" || this.rawValue == null)) {

    xfa.host.messageBox("Training agenda required");

}

View solution in original post

3 Replies

Avatar

Level 7

it is because you are using || which means 'or' for all the conditions so when the first condition is met (null) it doesn't even look at the last one (if the box is checked). You need to use && ('and') for the last condition to make it mandatory in the if statement.

Avatar

Level 3

Duh! I don't know why that didn't hit me before.

But it still didn't work!

I changed the code to:

if (this.rawValue == '' || this.rawValue == null && trainingCB =="1") {

    xfa.host.messageBox("Training agenda required");

}

Now the message doesn't pop up at all, even when the check box is checked.

I even tried enclosing the "or" statement in parenthesis, thinking that would help, but it made no difference:

if ((this.rawValue == '' || this.rawValue == null) && trainingCB =="1") {

    xfa.host.messageBox("Training agenda required");

}

Avatar

Correct answer by
Level 3

Figured it out. Two simple problems.

First, I wasn't using the && for and in my if statement. Second, I forgot to put .rawValue on something. So the corrected code looks like this:

if (trainingCB.rawValue == "1" && (this.rawValue == "" || this.rawValue == null)) {

    xfa.host.messageBox("Training agenda required");

}