These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

In Table, want field to become required if another field has text in it

Gillian
Registered: Jul 10 2007
Posts: 63
Answered

I have a table where Users can add or subtract rows as needed. If there is no data in the Part# field, then the Inventory Location field is optional. If there is text or numbers in the Part# field, then the Inventory Location field is required. Here's the code I tried to use on a change event but it failed:
 
var Empty = /^\s*$/;
if(Empty.test(event.value)){
this.getField("InvLoc").required = false;}
else{
this.getField("InvLoc").required = true;
}
 
I also tried the validation event but that caused the software to hang.
 
Can anyone supply me with the correct Javascript code?
 
Thanks in advance for your help
 
Gillian

-Gillian

My Product Information:
LiveCycle Designer, Windows
Bamboolian
Registered: Jul 27 2010
Posts: 48
Hi,

To make the field required, you can use property "mandatory".
Property will accept text values of the followings;

"error" : field requires value
warning : field is recommended to be filled
disabled : optional


Gillian
Registered: Jul 10 2007
Posts: 63
I changed required to mandatory in my script but it didn't work. Can you give me the entire code? Thanks!

-Gillian

SLDC
Registered: Oct 25 2010
Posts: 70
Did you use quotation marks around the values?

Example:
this.getField("InvLoc").mandatory = "error";
this.getField("InvLoc").mandatory = "disabled";

FYI, fields also have the property mandatoryMessage that lets you specify the error message.

Example:
this.getField("InvLoc").mandatoryMessage = "If you have entered a part number, you must enter an inventory location.");

Also - is there some reason you're using this.getField("InvLoc").mandatory instead of just InvLoc.mandatory ?
Gillian
Registered: Jul 10 2007
Posts: 63
This didn't work. I never got the error message and when I clicked Submit by Email, it allowed me to submit the form with the Inventory Location field not filled in.

What do you think the entire code should be. Maybe the error is in a different line.

Thanks in advance for your help.

-Gillian

Gillian
Registered: Jul 10 2007
Posts: 63
Here's what I tried this last time. Still didn't work. (Sorry my Javascript skills are so lame)

var myVal=null;
if (myVal!=null) {
InvLoc.mandatoryMessage = "If you have entered a part number, you must enter an inventory location.";
}
else
{
InvLoc.mandatory = "false";
}

I am doing this in a change event. Is that correct? Also, it's in a table. I don't want the user to be able to submit the form, via the Submit email button without having put in an inventory location

Thanks in advance for your help
-Gillian

-Gillian

Gillian
Registered: Jul 10 2007
Posts: 63
OK, I got it to work with the following script:

if (xfa.event.PartNum == null)
{
InvLoc.mandatory = "error"
}
else {InvLoc.mandatory = "disabled";}

Now I want to add a message. If they try to submit the form without filling in the Inventory Location field, I want a custome message instead of the generic, "At least one required field is empty..." How do I get my own message to display?

Thanks in advance!
Gillian

-Gillian

SLDC
Registered: Oct 25 2010
Posts: 70
After

InvLoc.mandatory = "error";

add

InvLoc.mandatoryMessage = "If you have entered a part number, you must enter an inventory location.";

See if that works.

Gillian
Registered: Jul 10 2007
Posts: 63
The problem with this InvLoc.mandatorymessage line is that after you type one character into the parts field, a dialog displays immediately with the message. It doesn't allow you to enter the full part# in the field. So I need the message to pop-up when someone clicks off of the field. How do I do that?

Thanks in advance,
Gillian

-Gillian

SLDC
Registered: Oct 25 2010
Posts: 70
Accepted Answer
Put your code in the Exit event instead of the Change event.