• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Validate Required Fields, Email PDF, and Reset Fields on Submit

New Here ,
Sep 10, 2024 Sep 10, 2024

Copy link to clipboard

Copied

Hi, friends!  I'm new to Acrobat and JS, and have searched the community but can't find quite the answer I'm looking for or can't get the scripts to work together.  

 

I have a simple form with 7 required fields (5 drop-downs, 2 free text if it matters).  Currently, I have the Submit button coded to validate the required fields and, if complete, send an email.  Script below. The problem is, if the required fields aren't complete, the Submit button does nothing and there's no error message or other indicator of what's missing.

 

When the Submit button is clicked, I need the following things to happen:

- Validate required fields and generate an error message if any are blank/left on default

- Flatten PDF/make all fields read-only (but data in fields must be copyable)

- Generate an email to the target email address with the flattened PDF attached (currently working)

- Clear all fields on the PDF

 

Is all this possible?  Any help/sample scripting (please make text I would replace super obvious 🙂 ) appreciated! 

 

TIA!

 

tm_tm_0-1726008108242.png

 

tm_tm_1-1726008182703.png

 

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms

Views

685

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
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

 

 

 

var count=0;
if(!this.getField("Division").value)
{app.alert("Division is a mandatory field.",1);count++;} else
if(!this.getField("Name").value)
{app.alert("Name is a mandatory field.",1);count++;} else
if(!this.getField("Date").value)
{app.alert("Date is a mandatory field.",1);count++;} else
if(!this.getField("Category").value)
{app.alert("Category is a mandatory field.",1);count++;} else
if(!this.getField("Total$Requested").value)
{app.alert("Total$Requested is a mandatory field.",1);count++;} else
if(!this.getField("CampaignRelated").value)
{app.alert("CampaignRelated is a mandatory field.",1);count++;}

if(count==0)
{
var targetEmail = "test@sample.com";
var subjectLine = "Reimbursement Request" + ": " + this.getField("Division").valueAsString + " - " + this.getField("Category").valueAsString + " - " + this.getField("Event Name Request Title").valueAsString + " - " + this.getField("Total $ Reimbursement Request").valueAsString;
this.mailDoc({cTo: targetEmail, cSubject: subjectLine});
}

 

 

 

If any of your fields are dropdowns and you used a space for an empty value, change !this.getField("fieldName").value to if(this.getField("fieldName").value ==" " for those fields.  Another thing that is helpful is to add, after each count++; is

this.getField("fieldName").setFocus();

Make sure you change "fieldName" to the actual field names.

View solution in original post

Votes

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
community guidelines
Community Expert ,
Sep 10, 2024 Sep 10, 2024

Copy link to clipboard

Copied

This script should be in a mouse up action, instead of a mouse enter action.  Making fields read only is not flattening them.  You can only flatten them if the end user as Acrobat Pro or Standard.  Reader will not flatten fields.  If you use read only instead, you won't be able to select the text in those fields to copy it.  If you are validating the values of dropdowns, the only way to make one of the selections no value is to use a script.  If you do it manually and make the no value a space, you will have validate for a space.

Please post the script for your function ValidateRequiredFields(this).

Votes

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
community guidelines
New Here ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Thanks!   I changed this to a mouse up action.   Most of our submitters will only have Reader.  My goal with the flatten/read only (I'm sure I'm using the wrong terminology) is to create a copy of what was submitted that can be referenced by both the submitter and the receiving team without having any fields intentionally or inadvertently changed.  Open to the best way to do that.  

My validation doesn't seem to be working so I'm probably missing something here. 

 

if (validateRequiredFields(this)) {

//

var targetEmail = "test@sample.com";

var subjectLine = "Reimbursement Request" + ": " + this.getField("Division").valueAsString + " - " + this.getField("Category").valueAsString + " - " + this.getField("Event Name Request Title").valueAsString + " - " + this.getField("Total $ Reimbursement Request").valueAsString;

this.mailDoc({cTo: targetEmail, cSubject: subjectLine});

}

Votes

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
community guidelines
New Here ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Clarification: I need the receiving team to be able to copy the data in the PDF fields to a different source but not change the data. 

Votes

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
community guidelines
Community Expert ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

What is the code for the function validateRequiredFields(this) that your script is referencing?

Votes

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
community guidelines
New Here ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

I think I may have missed a step somewhere, because the validate didn't work and I just took it out.  Here is the script now.  I have required fields, but there's nothing enforcing them now.

var targetEmail = "test@sample.com";

var subjectLine = "Reimbursement Request" + ": " + this.getField("Division").valueAsString + " - " + this.getField("Category").valueAsString + " - " + this.getField("Event Name Request Title").valueAsString + " - " + this.getField("Total $ Reimbursement Request").valueAsString;

this.mailDoc({cTo: targetEmail, cSubject: subjectLine});

Votes

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
community guidelines
Community Expert ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

If you want me to help you validate the required fields you will have to name them.  I could write a script to loop through all the fields and check for the required property, but that's overkill.

Votes

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
community guidelines
New Here ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Required fields are:

- Division

- Name

- Date

- Category

- Total$Requested

-CampaignRelated

Votes

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
community guidelines
Community Expert ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

 

 

 

var count=0;
if(!this.getField("Division").value)
{app.alert("Division is a mandatory field.",1);count++;} else
if(!this.getField("Name").value)
{app.alert("Name is a mandatory field.",1);count++;} else
if(!this.getField("Date").value)
{app.alert("Date is a mandatory field.",1);count++;} else
if(!this.getField("Category").value)
{app.alert("Category is a mandatory field.",1);count++;} else
if(!this.getField("Total$Requested").value)
{app.alert("Total$Requested is a mandatory field.",1);count++;} else
if(!this.getField("CampaignRelated").value)
{app.alert("CampaignRelated is a mandatory field.",1);count++;}

if(count==0)
{
var targetEmail = "test@sample.com";
var subjectLine = "Reimbursement Request" + ": " + this.getField("Division").valueAsString + " - " + this.getField("Category").valueAsString + " - " + this.getField("Event Name Request Title").valueAsString + " - " + this.getField("Total $ Reimbursement Request").valueAsString;
this.mailDoc({cTo: targetEmail, cSubject: subjectLine});
}

 

 

 

If any of your fields are dropdowns and you used a space for an empty value, change !this.getField("fieldName").value to if(this.getField("fieldName").value ==" " for those fields.  Another thing that is helpful is to add, after each count++; is

this.getField("fieldName").setFocus();

Make sure you change "fieldName" to the actual field names.

Votes

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
community guidelines
New Here ,
Sep 13, 2024 Sep 13, 2024

Copy link to clipboard

Copied

I previously had my drop-downs with the default "-Select Division-", "-Select Name-", etc.  I changed the defaults to a space/empty value as a default to use the code above and have the form error if the field is left on the default, but I must be missing something because now it's throwing an error on those fields no matter whether it's left on the default or an option is selected.  

 

Here's what I have now as the properties for the Division field and the updated Submit button code. Can you help me understand what I'm missing?  Thanks again!

 

tm_tm_0-1726238791372.png

 


var count=0;
if(!this.getField("Division").value=="")
{app.alert("Division is a mandatory field.",1);count++;} else
if(!this.getField("Name").value=="")
{app.alert("Name is a mandatory field.",1);count++;} else
if(!this.getField("Date").value)
{app.alert("Date is a mandatory field.",1);count++;} else
if(!this.getField("Category").value=="")
{app.alert("Category is a mandatory field.",1);count++;} else
if(!this.getField("Total$Requested").value)
{app.alert("Total Requested ($) is a mandatory field.",1);count++;} else
if(!this.getField("RequestTitle").value)
{app.alert("Event Name/Request Title is a mandatory field.",1);count++;} else
if(!this.getField("CampaignRelated").value=="")
{app.alert("Is this request related to an ACE campaign? is a mandatory field.",1);count++;}

if(count==0)
{

var targetEmail = "test@sample.com";
var subjectLine = "Reimbursement Request" + ": " + this.getField("Division").valueAsString + " - " + this.getField("Category").valueAsString + " - " + this.getField("Event Name Request Title").valueAsString + " - " + "$" + this.getField("Total $ Reimbursement Request").valueAsString;


var body =
"My reimbursement request is attached.";

this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});
}

 

 

 

 

Votes

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
community guidelines
Community Expert ,
Sep 13, 2024 Sep 13, 2024

Copy link to clipboard

Copied

For text fields the script should be if(!this.getField("TextField").value)

For dropdowns it should be if(this.getField("Dropdown").value == " ")

Note the space between the quotes in the line above and the missing !.

Votes

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
community guidelines
New Here ,
Sep 13, 2024 Sep 13, 2024

Copy link to clipboard

Copied

"Division" is a drop-down.  
"Name" is a variable drop-down based on the Division selection (code below. This part has worked.  I previously had the default on each variable list as "-Select Name-" but have changed it to a space as with the others.).

"Date" is a calendar picker.

"Category" is a drop-down.

"Total$Requested" is free text.

"RequestTitle" is free text.
"CampaignRelated" is a drop-down.

 

"Division" field Format > Custom Keystroke Script to drive "Name" drop-down:

 

var Itemlist = this.getField("Name");

if(event.willCommit)
{
switch(event.value){

case "NE":
Itemlist.setItems(["", "Name1", "Name2"]);
break;

case "SE":
Itemlist.setItems(["", "Name3", "Name4"]);
break;

case "GC":
Itemlist.setItems(["", "Name5, "Name6"]);
break;

case "CT":
Itemlist.setItems(["", "Name7", "Name8"]);
break;

case "WT":
Itemlist.setItems(["", "Name9", "Name10"]);
break;

default:
Itemlist.setItems([""]);
break;

}

}

 

Votes

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
community guidelines
Community Expert ,
Sep 13, 2024 Sep 13, 2024

Copy link to clipboard

Copied

LATEST

If the default dropdown item was set with a script as above, and is "", !this.getField("Name").value will work.  If the default dropdown item is a space because it was set manually in the field properties the script should be this.getField("Name").value == " " The first one translates to "has no value", the 2nd one translates to "value is equal to a space".  It's ok to use -Select Name-, but you would have to modify the script to this.getField("Name").value=="-Select Name-"

Votes

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
community guidelines