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

Explorer ,
Feb 01, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

386

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

Explorer ,
Feb 01, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

387

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 01, 2020 0
Adobe Community Professional ,
Feb 01, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 01, 2020 0
Explorer ,
Feb 04, 2020

Copy link to clipboard

Copied

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.  

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Most Valuable Participant ,
Feb 04, 2020

Copy link to clipboard

Copied

Add this to your code:

 

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Explorer ,
Feb 04, 2020

Copy link to clipboard

Copied

First, thank you!  I believe that you are the one who helped me make the above code work in the first place.

 

Second:  This is what I did:

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==0) event.rc = false;

 

I feel like it's wrong because it didn't work....  

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Most Valuable Participant ,
Feb 04, 2020

Copy link to clipboard

Copied

Works fine for me. However, it also rejects empty values, which might not be desired.

To prevent that change it to:

if (event.value!="" && event.value==0) event.rc = false;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Explorer ,
Feb 04, 2020

Copy link to clipboard

Copied

Okay.  Changed the last line to the new one from you last post.

I'm still being allowed to enter a 0 and move on after the field has become required...

Am I missing a setting or something...?  Is the code supposed to go somewhere else instead of being added to Calc Script?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Most Valuable Participant ,
Feb 04, 2020

Copy link to clipboard

Copied

Yes, it needs to be a Validation script. Sorry, I thought that's where you placed the rest of the code.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Explorer ,
Feb 04, 2020

Copy link to clipboard

Copied

Okay.  Here's what I've run into.

I placed the entire code into the Validation Script and it didn't function without returning to the field to make it work.

When I separate them and leave the 'required' code in the Calc section and place your new line in the Validation section, it doesn't allow 0 to be input at all.  But, as with before, having both pieces in the Calc section doesn't restrict the 0 at all.

The desire here is to only prevent 0 from being input when the field is required.  All other times, 0 is allowed to be input.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Most Valuable Participant ,
Feb 04, 2020

Copy link to clipboard

Copied

Ah, that was not clear... In that case, use this:

if (event.value!="" && event.value==0 && event.target.required) event.rc = false;

 

The rest of the code should remain as the custom calculation script.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Explorer ,
Feb 04, 2020

Copy link to clipboard

Copied

You are a god among men, sir!  I new it could be done!  Thank you so much.  That worked perfectly.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 04, 2020 0
Explorer ,
Feb 05, 2020

Copy link to clipboard

Copied

I tested this out a bit more this morning.  What I'm finding is that, while it allows the 0 to be input when the field isn't required, it is still displaying the app alert that I put in to inform the user when it is required why it isn't allowed.  Additionally, I'm finding that when it is already filled in with 0 and the drop downs are changed, causing it to become required, it isn't triggering anything telling the user that 0 isn't allowed.

Is there a way around that...?  Or is it just a casualty of the situation in which I find myself?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 05, 2020 0
Explorer ,
Feb 07, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 07, 2020 0