Need help for javascript for validating checkboxes before submitting form

New Here ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Hi! I have been asked to create a couple of PDF fillable forms. The users wanted some 'features' that require running scripts; I have 0 background in javascripts. I have been lucky enough to find sources on the internet and they have helped me a lot (special mention to Mr. Thom Parker's DynamicEmail_AcroForm - thank you!). I have used Mr. Parker's script for validating that all required fields are filled out before the form can be emailed. However, the script does not include validating checkboxes. The email button has the following script:

 

if(FieldValidation1())
{

var cToAddr = this.getField("Manager's Email Address").value;
var cSubLine = "For Approval: Aspiring Leaders Program Nomination Form"
var cBody = "Hi. Please find attached the completed nomination form for your approval.\n\n" + "Thank you."
this.mailDoc({bUI: true, cTo: cToAddr, cSubject: cSubLine, cMsg: cBody});

}

 

Following is the script for FieldValidation1, located in the Document JavaScripts:

function FieldValidation1()
{
var bRtn = false;
var aErrMsg = [];
var rgEmpty = /^\s*$/;
if(rgEmpty.test(this.getField("Name").value))
aErrMsg.push("Name");
if(rgEmpty.test(this.getField("Email Address").value))
aErrMsg.push("Email Address");
if(rgEmpty.test(this.getField("Classification").value))
aErrMsg.push("Classification");
if(rgEmpty.test(this.getField("Job Title").value))
aErrMsg.push("Job Title");
if(rgEmpty.test(this.getField("Business Unit").value))
aErrMsg.push("Business Unit");
if(rgEmpty.test(this.getField("Start Date").value))
aErrMsg.push("Start Date");
if(rgEmpty.test(this.getField("Question 1").value))
aErrMsg.push("Question 1");
if(rgEmpty.test(this.getField("Question 2").value))
aErrMsg.push("Question 2");
if(rgEmpty.test(this.getField("Question 3").value))
aErrMsg.push("Question 3");
if(rgEmpty.test(this.getField("Manager's Email Address").value))
aErrMsg.push("Manager's Email Address");
if(aErrMsg.length == 0)
bRtn = true;
else
app.alert("One or more required fields have not been filled out:\n\n * " + aErrMsg.join("\n * "));;
return bRtn;

}

 

I have 6 checkboxes on the form after Questions 1 to 3 that need to be ticked before the form can be emailed. Can someone please help me with the script? Many thanks.

 

TOPICS
JavaScript

Views

56

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

correct answers 1 Correct answer

Adobe Community Professional , Mar 02, 2022 Mar 02, 2022

Change these lines:

 

function FieldValidation1()
{
var bRtn = false;
var aErrMsg = [];
var rgEmpty = /^\s*$/;
if(rgEmpty.test(this.getField("Name").value))
aErrMsg.push("Name");
if(rgEmpty.test(this.getField("Email Address").value))
aErrMsg.push("Email Address");
if(rgEmpty.test(this.getField("Classification").value))
aErrMsg.push("Classification");
if(rgEmpty.test(this.getField("Job Title").value))
aErrMsg.push("Job Title");
if(rgEmpty.test(this.getField("Business Unit").value))
aErrMsg.push("Business Unit"

...

Likes

Translate

Translate
Adobe Community Professional ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Change these lines:

 

function FieldValidation1()
{
var bRtn = false;
var aErrMsg = [];
var rgEmpty = /^\s*$/;
if(rgEmpty.test(this.getField("Name").value))
aErrMsg.push("Name");
if(rgEmpty.test(this.getField("Email Address").value))
aErrMsg.push("Email Address");
if(rgEmpty.test(this.getField("Classification").value))
aErrMsg.push("Classification");
if(rgEmpty.test(this.getField("Job Title").value))
aErrMsg.push("Job Title");
if(rgEmpty.test(this.getField("Business Unit").value))
aErrMsg.push("Business Unit");
if(rgEmpty.test(this.getField("Start Date").value))
aErrMsg.push("Start Date");
if(rgEmpty.test(this.getField("Question 1").value))
aErrMsg.push("Question 1");
if(rgEmpty.test(this.getField("Question 2").value))
aErrMsg.push("Question 2");
if(rgEmpty.test(this.getField("Question 3").value))
aErrMsg.push("Question 3");

if(this.getField("Question 1").value == this.getField("Question 1").defaultValue)
aErrMsg.push("Question 1");
if(this.getField("Question 2").value == this.getField("Question 2").defaultValue)
aErrMsg.push("Question 2");

if(this.getField("Question 3").value == this.getField("Question 3").defaultValue)
aErrMsg.push("Question 3");

if(rgEmpty.test(this.getField("Manager's Email Address").value))
aErrMsg.push("Manager's Email Address");

if(aErrMsg.length == 0)
bRtn = true;
else
app.alert("One or more required fields have not been filled out:\n\n * " + aErrMsg.join("\n * "));;
return bRtn;

}

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
community guidelines
New Here ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

LATEST

Thank you so much JR Boulay! I can't thank you enough! Much appreciated!

Apologies for the late reply. Your message went to my junk mail.

 

 

 

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