Copy link to clipboard
Copied
Hi
I’m creating a form whereby I need certain fields to become active and Required, dependant on entry into other fields.
For instance, if I have a drop down menu called “Choice” with 3 options (the default option is always ‘Pleae Select’ with an export value of 1) and a field called “Description”, which must become active and mandatory if a choice is made, I set the “Description” field as Required but use the following Calculation script:
if (this.getField(“Choice”).value == “1”) {
event.target.display = display.hidden;
}
else {
event.target.display = display.visible;
}
This works great. However, I do not know how to achieve the above based on a users entry of free text into a Text Field, rather than based on their selection from a drop down menu.
For example, if I have a Text Field called “Address” which users can freely enter text into, how would I make a field called “AddressDescription” become active only if they enter text into the address field, and of course become hidden again if they decide to delete the address text? I don’t know how field values work when it comes to entry of text.
Help on this would be hugely appreciated!
Copy link to clipboard
Copied
When the value of a field is empty it equals the empty string, ie "".
When it's not empty it doesn't equal it. So you can do it like this:
if (this.getField("Text1").value == "") {
event.target.display = display.hidden;
event.target.required = false;
} else {
event.target.display = display.visible;
event.target.display = true;
}
Copy link to clipboard
Copied
Further to the above, I’ve just realised that Required fields still act as being Required even if they are hidden - doh.
So I’ve amended my current code to the following instead:
if (this.getField(“Choice”).value == “1”) {
event.target.display = display.hidden;
event.target.required = false;
}
else {
event.target.display = display.visible;
event.target.display = true;
}
So yeah, basically I need to know how to achieve the above based on users input into a text field, rather than from making a selection.
many thanks in advance
Copy link to clipboard
Copied
Exactly the same way... The value of a text field can be accessed using its value property.
Copy link to clipboard
Copied
Hi - thanks for replying.
Unfortunately I do not understand
I don’t know what the value of an empty Text Field is, and I don’t know what the value of that field becomes when text is entered into it?
I looked at putting a figure in the ‘default value’ box of the Options tab in the field properties, but doing so places a number in the field, which obviously I want to remain blank.
Are you able to provide an example?
Let’s say I have a Text called field called “Name” and a Text field called “DOB”; I need the DOB field to become active and/or required if a name is typed into the Name field.
If somone types Joe Bloggs into the Name field, what value does that field now have??
Sorry for being a n00b - I really don’t know JavaScript but looking into doing a training course ASAP!
Copy link to clipboard
Copied
When the value of a field is empty it equals the empty string, ie "".
When it's not empty it doesn't equal it. So you can do it like this:
if (this.getField("Text1").value == "") {
event.target.display = display.hidden;
event.target.required = false;
} else {
event.target.display = display.visible;
event.target.display = true;
}
Copy link to clipboard
Copied
try67 - that is incredibly valuable information - thank you so much! Simple really I guess.
One last question for you..
I’ve been been using this as custom calculation script, and it’s been working just fine. However is this correct or should I be placing it in as a custom validation script and/or what is the difference??
many thanks once again
Copy link to clipboard
Copied
If you don't want these fields to have a calculated value then you can keep using it as a calculation script. If you do then you could move it to the validation script, yes.
The difference is a bit complicated, but in general terms a calculation script is used to assign a value to a script field and a validation script is used to validate that the field's value adheres to certain rules. We're "hijacking" them for other purposes, though, and that's pretty common.
Copy link to clipboard
Copied
Show Field Base on Entry
I am creating a purchase request document with a lot of required fields but, not every field is required for every purchase. I need to have the field "Account" display and be required, based on a number entered into the field "Num". The Num fields Common Properties are set to Visible and Required and the Format is Set to Number. I entered the below code into the Calculate tab of the "Account" field. I was expecting the Account field to display and be required when the number was entered but, that didn't happen.
if (this.getField("Num").value == "") {
event.target.display = display.hidden;
event.target.required = false;
} else {
event.target.display = display.visible;
event.target.display = true;
}

