Skip to main content
ZombieLuna
Inspiring
February 1, 2020
Question

Javascript Where If Text Field is Required, it Cannot Equal 0

  • February 1, 2020
  • 2 replies
  • 3326 views

I have a text field whose required status is based on a selection made in a drop down.  What I need to add to the code that I've already got (see below):

 

if (this.getField("Bedrooms").value=="Points"){event.target.required=true}
else if (this.getField("Bathrooms").value=="Points"){event.target.required=true}
else if (this.getField("Occupancy").value=="Points"){event.target.required=true}
else
{event.target.required=false}

 

Is that if this field is required it cannot equal 0.  The problem is that any other time, it can be 0.  I don't doubt that it is doable.  I just don't have the knowledge to make it happen...  Any help would be appreciated.

This topic has been closed for replies.

2 replies

ZombieLuna
Inspiring
February 7, 2020

Okay.  I've made a few adjustments to the super helpful answers that I received in this thread and what I have now works...but not *exactly* how I would like it to.

Using this code in the Custom Calculation Script section:

if (this.getField("Bedrooms").value=="Points"){event.target.required=true}
else if (this.getField("Bathrooms").value=="Points"){event.target.required=true}
else if (this.getField("Occupancy").value=="Points"){event.target.required=true}
else
{event.target.required=false}

and this code in the Custom validation script section:

if (event.value==0 && event.target.required) {
event.value = "";
app.alert("If unit size is based on points, Points cannot be 0.");
event.rc = false;
}

the Points field allows 0 to be input when the Unit Size fields are not set to points.  It also, prevents 0 from being input and clears out the field when the fiels is blank and the Unit Size fields are set to points. 

The trouble I'm having now is when the points field is filled out with a 0 and the Unit Size drop downs get set to points, the field doesn't clear OR alert the user that the field cannot be 0.

 

If I put this code in the Custom Calculation Script section:

if (this.getField("Bedrooms").value=="Points"){event.target.required=true}
else if (this.getField("Bathrooms").value=="Points"){event.target.required=true}
else if (this.getField("Occupancy").value=="Points"){event.target.required=true}
else
{event.target.required=false}

if (event.value!="" && event.value==0 && event.target.required) {
    app.alert("If unit size is based on points, Points cannot be 0.");
    event.rc = false;
}

then the Points field being filled in with 0 when the Unit Size drop downs get set to points does trigger the app alert, however it doesn't clear out the field.

 

Is there a way to have what is happening still work AND have the field clear if it is filled in with 0 and the unit size drop downs get set to points after the fact?

Thom Parker
Community Expert
Community Expert
February 1, 2020

The answer depends. 

Where is the script you posted above? Is it a calcuation?

So if the value is not 0, then what is it? 

How is the field value set in the first place?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ZombieLuna
Inspiring
February 4, 2020

Gasp!  I'm sorry.

That code is a Custom Calculation Script applied to the Points field.  It pulls from three dropdowns (Bedrooms, Bathrooms, and Occupancy).  If any of those fields have Points selected, it makes the Points field required before the user can save or print the file.  If anything else is selected, Points being filled in is optional.  It can be 0 or blank.

However, my goals is to ask Adobe to recognize when that field is required and restrict the user from putting in 0 as the amount of points, as that is not an acceptable answer.  

try67
Community Expert
Community Expert
February 4, 2020

Add this to your code:

 

if (event.value==0) event.rc = false;