Skip to main content
Vortex99
Participating Frequently
July 23, 2019
Answered

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

  • July 23, 2019
  • 1 reply
  • 751 views

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.

This topic has been closed for replies.
Correct answer try67

No, text fields do not corresponds to check boxes.


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

}

1 reply

try67
Community Expert
Community Expert
July 23, 2019

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

Vortex99
Vortex99Author
Participating Frequently
July 23, 2019

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.

try67
Community Expert
Community Expert
July 23, 2019

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