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

Add Condition to Submit Button Based on Required Fields

New Here ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript , Windows

Views

3.1K

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

Community Expert , Sep 01, 2017 Sep 01, 2017

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

}

Votes

Translate

Translate
Community Expert ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

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.

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

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.

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

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")

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

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!

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

I meant you should use something like this:

if (fld6.valueAsString == fld6.defaultValue)

And I don't follow your last request about a single output... Do you mean a single error message, instead of one for each field?

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

Ok great! That worked.

I think I have an issue with my if statement now:

if (fld1.value != "" && fld2.value != "" && fld3.value != "" && fld4.value != "" && fld5.value != "" && fld6.value != "")

Because with using the defaulvalue it still executes the if statement if there is an error with fld6. Do I need to change that so that it only goes if the value == "Yes"?

And as far as output messages, you are exactly correct. If there are say, 2 errors, instead of getting two separate messages, the ideal state would be getting one message with both errors.

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

You should use defaultValue (notice the capital V!) there as well.

As for the combined message, it's possible, but then which field should get the focus?

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

That worked beautifully!! Thank you!!

Honestly the form is short enough that set focus isn't absolutely necessary. So even if it always defaulted back to fld1 would be fine.

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

Post your current code and I'll help you implement it.

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

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!

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

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

}

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 01, 2017 Sep 01, 2017

Copy link to clipboard

Copied

You are amazing! It works beautifully! Thank you so much!

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 ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

I'm trying to do something pretty basic - or most people will think it's basic (I'm a novice).  I have a form with required fields, but I have 2 boxes that will determine if a 3rd field is required.  Example:  my form is to add or change a form.  There is a check box for "Add" and a check box for "Change".  If the form is to change an existing account then the "vendor number" is going to be a required field.  Could you possibly help me write the script for this to work?

Any help is greatly appreciated.

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 ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

Are these check-boxes in the same group? What are their export values?

Assuming they are, and are called "add/change" and the values are "Add" and "Change", create a hidden text field with this calculation code:

this.getField("vendor number").required = (this.getField("add/change").valueAsString=="Change");

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 ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

The "add" "change" boxes are not in the same group.  They are separate boxes so someone can check either one.  Would you be able to help me write a script for that?

Thanks!

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 ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

In that case the "Add" box is irrelevant. What's the name and "on" value of the "Change" box?

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 ,
Jun 03, 2018 Jun 03, 2018

Copy link to clipboard

Copied

I get the following error

TypeError: this.getField(...) is null

1:Field:Calculate

TypeError: this.getField(...) is null

1:Field:Calculate

Not sure what I'm doing wrong.  Thanks!

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 ,
Jun 03, 2018 Jun 03, 2018

Copy link to clipboard

Copied

LATEST

Youv'e entered an incorrect field name in the first line of your code. Remember that JS is case-sensitive, so if the field is called "Change" it won't work if you enter "change" in your code.

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