Required minimum 2 text fields out of 4 to be completed before submiting a form

Community Beginner ,
Jul 23, 2019

Copy link to clipboard

Copied

Hi all. Need some help if possible. I have a submit button in my form running a Java Script to extract emails associated to checkboxes in that particular form. So this works like this:

var fields = ["Checkbox1", "Checkbox2", "Checkbox3", "Checkbox4"];

var addresses = [];

for (var i in fields) {

    var v = this.getField(fields).valueAsString;

    if (v!="Off") addresses.push(v);

}

  1. this.mailDoc({cTo: addresses.join(";"), cSubject: "Subject line goes here"});

And each checkbox has an email address as export value. This is all working fine. Now with all of this working I'm wondering if there is a chance to do the next:

- form has 4 required text fields. I want that user must fill up minimum 2 out of these 4 fields, before Submit button will work. Of course it is allowed to user to fill 3 or even all 4 required text fields and then to submit a form as well. Now I know that there is a validation script for required either text field 1 or 2 to be completed:

  1. this.getField("Name of other field").required = (event.value=="");

  1. event.target.required = true;

and set your fields as required, but then I have to run a Submit form on a submit button without possibility to extract emails associated to checkboxes. Hope all is clear, let me know if you need any other info.

Tnx to all in advance.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

OK... Then you can do it like this, I guess:

var fields1 = ["Text1", "Text2", "Text3", "Text4"];

var addresses1 = [];

for (var i in fields1) {

    var v = this.getField(fields1).valueAsString;

    if (v!="") addresses1.push(v);

}

if (addresses1.length<2) app.alert("Error!");

else {

    // place the rest of the code to submit the file here

}

TOPICS
Acrobat SDK and JavaScript, Windows

Views

85

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

Required minimum 2 text fields out of 4 to be completed before submiting a form

Community Beginner ,
Jul 23, 2019

Copy link to clipboard

Copied

Hi all. Need some help if possible. I have a submit button in my form running a Java Script to extract emails associated to checkboxes in that particular form. So this works like this:

var fields = ["Checkbox1", "Checkbox2", "Checkbox3", "Checkbox4"];

var addresses = [];

for (var i in fields) {

    var v = this.getField(fields).valueAsString;

    if (v!="Off") addresses.push(v);

}

  1. this.mailDoc({cTo: addresses.join(";"), cSubject: "Subject line goes here"});

And each checkbox has an email address as export value. This is all working fine. Now with all of this working I'm wondering if there is a chance to do the next:

- form has 4 required text fields. I want that user must fill up minimum 2 out of these 4 fields, before Submit button will work. Of course it is allowed to user to fill 3 or even all 4 required text fields and then to submit a form as well. Now I know that there is a validation script for required either text field 1 or 2 to be completed:

  1. this.getField("Name of other field").required = (event.value=="");

  1. event.target.required = true;

and set your fields as required, but then I have to run a Submit form on a submit button without possibility to extract emails associated to checkboxes. Hope all is clear, let me know if you need any other info.

Tnx to all in advance.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

OK... Then you can do it like this, I guess:

var fields1 = ["Text1", "Text2", "Text3", "Text4"];

var addresses1 = [];

for (var i in fields1) {

    var v = this.getField(fields1).valueAsString;

    if (v!="") addresses1.push(v);

}

if (addresses1.length<2) app.alert("Error!");

else {

    // place the rest of the code to submit the file here

}

TOPICS
Acrobat SDK and JavaScript, Windows

Views

86

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
Jul 23, 2019 0
Most Valuable Participant ,
Jul 23, 2019

Copy link to clipboard

Copied

Setting the fields as required is not going to work.

What you can do is simply change the last line of the submit code to this:

if (addresses.length>=2) this.mailDoc({cTo: addresses.join(";"), cSubject: "Subject line goes here"});

else app.alert("You must select at least two email addresses.",1);

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...
Jul 23, 2019 0
Community Beginner ,
Jul 23, 2019

Copy link to clipboard

Copied

Ty for your quick reply try67.

This is working great, but I had something else on mind. Sorry if I was unclear in first place.

With your addition to the code, an alert message pops up, if user tries to submit a form, without previously selecting at least two  checkboxes. This is working just fine.

But in my form I have 4 other text fields e.g. ("Text1", "Text2", "Text3" , "Text4").

It's mandated that minimum 2 out of those 4 fields are filled in, before user is able to submit a form

e.g.

if "Text1" & "Text2" are filled, and "Text3" & "Text4" left blank; user is able to submit a form

or

if "Text1" & "Text3" are filled, and "Text2" & "Text4" left blank; user is able to submit a form

or

if "Text2" & "Text4" are filled, and "Text1" & "Text3" left blank; user is able to submit a form

and so on.

And if user enters value in only one text filed or leave all of these 4 text fields blank than form can not be submitted.

Tnx again, hope all of this makes sense.

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...
Jul 23, 2019 0
Most Valuable Participant ,
Jul 23, 2019

Copy link to clipboard

Copied

And each text field corresponds to a check-box, then?

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...
Jul 23, 2019 0
Community Beginner ,
Jul 23, 2019

Copy link to clipboard

Copied

No, text fields do not corresponds to check boxes.

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...
Jul 23, 2019 0
Most Valuable Participant ,
Jul 23, 2019

Copy link to clipboard

Copied

OK... Then you can do it like this, I guess:

var fields1 = ["Text1", "Text2", "Text3", "Text4"];

var addresses1 = [];

for (var i in fields1) {

    var v = this.getField(fields1).valueAsString;

    if (v!="") addresses1.push(v);

}

if (addresses1.length<2) app.alert("Error!");

else {

    // place the rest of the code to submit the file here

}

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...
Jul 23, 2019 0
Vortex99 LATEST
Community Beginner ,
Jul 23, 2019

Copy link to clipboard

Copied

Thanks a bunch. This is working like a charm. Thanks for all your help.

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...
Jul 23, 2019 0