Skip to main content
Wallenbees
Participating Frequently
August 31, 2017
Answered

Add Condition to Submit Button Based on Required Fields

  • August 31, 2017
  • 2 replies
  • 4115 views

Hey all. I have been scouring forums all morning for an answer to this but keep coming up empty. First of all, I am still VERY novice at javascript in Acrobat, but here is the scenario:

I have 5 required fields in my form. When I have a submit button that just uses the mouse up - submit form event, it will reject the submission if a required field is empty. However, I am submitting using javascript so that I can add text from one of the fields to the subject of the email that this is submitted by. The issue is when it is submitted this way, it no longer seems to validate the form to ensure the required fields are filled.

Some addition context:

The required fields are largely text and are as follows:

repname (text)

pneumonic (text)

customer id (text)

client name (text)

Verification Method1 (drop down)

If all of these are filled out then the form should allow submission. In a perfect world, if they click submit and any of the fields are empty I would love a pop up message that lets them know that field is missing and required.

If it is easier, I am ok with having the button be read only until those fields are filled. I feel like this should be really easy, but I can't seem to make it work. What do you all think?

This topic has been closed for replies.
Correct answer try67

var fld1 = this.getField('schwabrepname');
var fld2 = this.getField('Pneumonic');
var fld3 = this.getField('Customer ID');
var fld4 = this.getField('Verification Method1');
var fld5 = this.getField('Client Name');
var fld6 = this.getField('Suitability Group');
{
if (fld1.value == "")
{
app.alert("Please enter the name of the Submitting Representative.");
fld1.setFocus();
}
if (fld2.value == "")
{
app.alert("Please enter your Branch Pneumonic");
fld2.setFocus();
}
if (fld3.value == "")
{
app.alert("Please enter a Customer ID.");
fld3.setFocus();
}
if (fld4.valueAsString == fld4.defaultValue)
{
app.alert("Please include client verification method.");
fld4.setFocus();
}
if (fld5.value == "")
{
app.alert("Please provide client name.");
fld5.setFocus();
}
if (fld6.valueAsString == fld6.defaultValue)
{
app.alert("Suitability must be updated in the previous 12 months to request a prefilled application.");
fld6.setFocus();
}

if (fld1.value != "" && fld2.value != "" && fld3.value != "" && fld4.value != "" && fld5.value != "" && fld6.value != defaultValue)
{
var r = app.alert("Please note by submitting this form you are confirming all information is accurate and all required fields have been completed. Do you want to submit?",2,2);
if (r==4){
this.mailDoc({bUI:false, cTo:email@email.com,
cSubject: this.getField("Pneumonic").value + " Prefill Request" ,
cMsg: "Please complete the prefill request attached to this email.",
cSubmitAs: "PDF"});
};
}
}

Here is the current code. Thank you so much for all the help!


Use this:

var errMsgs = [];

var firstBadField = null;

if (fld1.valueAsString == fld1.defaultValue) {

    errMsgs.push("Please enter the name of the Submitting Representative.");

    firstBadField = fld1;

}

if (fld2.valueAsString == fld2.defaultValue) {

    errMsgs.push("Please enter your Branch Pneumonic.");

    firstBadField = fld2;

}

// etc.

if (errMsgs.length>0) {

    app.alert(errMsgs.join("\n\n"));

    firstBadField.setFocus();

} else {

    // insert the code to mail the form here

}

2 replies

Wallenbees
Participating Frequently
September 1, 2017

Here is what I was able to create. It all works with the exception of the 4th and 6th fields:

var fld1 = this.getField('repname');
var fld2 = this.getField('Pneumonic');
var fld3 = this.getField('Customer ID');
var fld4 = this.getField('Verification Method1');
var fld5 = this.getField('Client Name');
var fld5 = this.getField('Group2');
{
if (fld1.value == "")
{
app.alert("Please enter the name of the Submitting Representative.");
fld1.setFocus();
}
if (fld2.value == "")
{
app.alert("Please enter your Branch Pneumonic");
fld2.setFocus();
}
if (fld3.value == "")
{
app.alert("Please enter a Customer ID.");
fld3.setFocus();
}
if (fld4.value == "[Select]")
{
app.alert("Please include client verification method.");
fld4.setFocus();
}
if (fld5.value == "")
{
app.alert("Please provide client name.");
fld5.setFocus();
}
if (fld6.value = "No")
{
app.alert("Suitability must be updated in the previous 12 months to request a prefilled application.");
fld6.setFocus();
}

if (fld1.value != "" && fld2.value != "" && fld3.value != "" && fld4.value != "" && fld5.value != "" && fld6.value != "Yes")
{
var r = app.alert("Please note by submitting this form you are confirming all information is accurate and all required fields have been completed. Do you want to submit?",2,2);
if (r==4){
this.mailDoc({bUI:false, cTo:email@email.com,
cSubject: this.getField("Pneumonic").value + " Prefill Request" ,
cMsg: "Please complete the prefill request attached to this email.",
cSubmitAs: "PDF"});
};
}
}

What am I doing wrong? Additionally, if there are multiple items incorrect is it possible to include them all on one message?

EDIT: Important information - Field 4 is a drop down and Field 6 is a radio button. I think the fact that they aren't text boxes is throwing it off.

try67
Community Expert
Community Expert
September 1, 2017

You should compare the values to the defaultValue property, instead of a hard-coded one.

Also, you have an error in this line of code:

if (fld6.value = "No")

It should be:

if (fld6.value == "No")

Wallenbees
Participating Frequently
September 1, 2017

I hate to say I don't quite understand that first line of feedback. At first I thought you meant "2" instead of "No" on the Yes/No radio button, but that didn't fix anything. What should the default values look like in these lines?

Also do you have any insight in combining those messages to a single output?

I fixed the error you noticed. Thank you so much for the help!

try67
Community Expert
Community Expert
August 31, 2017

It's certainly possible to validate the required fields using a script. Try searching around the forums. This issue was discussed many times and I've provided code that does it to multiple people.