Copy link to clipboard
Copied
Hi Everyone,
I just downloaded Adobe Pro DC and was creating a form that has 3 checkboxes named (DAK, DHS, EC). I need the client to be required to have at least one checked and I don't know the script to write to make that happen. I've looked through a couple different threads and can't find an answer that works for mine. Any help would be greatly appreciated!
Copy link to clipboard
Copied
Required for what purpose, exactly?
Copy link to clipboard
Copied
There is a submit button on the form and before the client clicks submit I need them to select at least one of the three checkboxes
Copy link to clipboard
Copied
There are a couple of ways of doing it. One is to create a hidden (and required) text field that copies the value of the check-boxes. If you set the default value of the field as "Off" then it will fail the required fields validation when no check-box is clicked. The downside is that the field won't be visible so the user might have a hard time knowing what value is missing.
The other option is to use a script to perform the validation. Myself (and others) have posted sample code on these forums so it's shouldn't be too difficult to find it. The upside is that you can show any validation message you want, but it requires a bit more work to set it up.
Copy link to clipboard
Copied
I think the script would be best. I had a hard time finding it, could you send me the link or script so I can test it?
Copy link to clipboard
Copied
Here's the basic version of this code (use your own email address in the last line, of course):
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));
} else this.mailDoc({cTo: "me@server.com"});
Copy link to clipboard
Copied
It doesn't seem to work. When I push the submit button I can still submit the form even if none of the check boxes are checked. Any ideas on where to start?
Copy link to clipboard
Copied
You have to make sure that only the JS action is associated with the button. Remove any other "Submit form" actions you used before.
Copy link to clipboard
Copied
There are several other fields in the form that need to be required as well. It's almost like a credit card form; check which type of credit card (amex, visa, mastercard, etc) the credit card number field and expiration date are fillable fields that are required before hitting the submit button.
Copy link to clipboard
Copied
It's not contradictory. The script posted above will validate all fields set as required.
Copy link to clipboard
Copied
Ok, I've taken out the other submit/mail-to function and taken out any other pre-existing required fields from the way I had them before. I put the following code in the each check box (Properties>Actions>Mouse Up/Run a JavaScript) with my own email. It still isn't setting anything as required.
var emptyFields = [DAK, DHS, EC];
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));
} else this.mailDoc({cTo: "me@server.com"});
Copy link to clipboard
Copied
This code doesn't set anything as required. That's something you need to do.
It just validates the fields that are set as required and makes sure they are filled-in before submitting the file.
Copy link to clipboard
Copied
Ah! Ok, that is what I was asking for. I'm trying to figure out how to get them to be required when someone first opens it and after they click at least one checkbox I will be able to submit the form. If they don't select at least one check box it should allow them to submit the form until they do.
Copy link to clipboard
Copied
We're going in circles here... The required property is something you specify in advance via the field's properties.
The script validates that the fields you've defined as required are filled-in when the form is submitted.
Copy link to clipboard
Copied
Correct. However my problem is if I have all three check boxes required, then they must select all three before clicking submit. I don't want them to do that, I only want them to be required to select at least one.
Copy link to clipboard
Copied
But do you want them to be able to select more than one, in that group of three? If so then it's a much more complex situation and would require a more complex validation script.
Copy link to clipboard
Copied
Any update on hwo to do this?
It should really be an option within Acrobat.
Let's say there are 10 yes or no questions, and a few multiple choice questions which must ahve one (and only one) box checked. Will someone prompt Chat GPT (or a community expert) to make this validation script?
Copy link to clipboard
Copied
Go for it... Post here what it comes up with, it will be interesting to see.
Copy link to clipboard
Copied
Where only one checkbox can be selected and at least one must be selected, you can use radio buttons instead of checkboxes. Radio buttons ensure that only one option can be selected at a time, and you can set one of them as the default selection to guarantee that at least one is selected.
However, if you still prefer using checkboxes, you can achieve the desired behavior using JavaScript. Here's a script that you can use in your Adobe PDF form:
// Get the checkboxes in the group
var checkBox1 = this.getField("checkbox1");
var checkBox2 = this.getField("checkbox2");
var checkBox3 = this.getField("checkbox3");
// Function to check if at least one checkbox is selected
function isAtLeastOneChecked() {
return checkBox1.valueAsString != "Off" || checkBox2.valueAsString != "Off" || checkBox3.valueAsString != "Off";
}
// Function to uncheck other checkboxes when one is selected
function uncheckOthers(selectedCheckbox) {
var checkboxes = [checkBox1, checkBox2, checkBox3];
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i] != selectedCheckbox) {
checkboxes[i].checkThisBox(0, false);
}
}
}
// Add this to the MouseUp event of each checkbox
checkBox1.setAction("MouseUp", "if (event.target.value != 'Off') { uncheckOthers(checkBox1); }");
checkBox2.setAction("MouseUp", "if (event.target.value != 'Off') { uncheckOthers(checkBox2); }");
checkBox3.setAction("MouseUp", "if (event.target.value != 'Off') { uncheckOthers(checkBox3); }");
// Add a validation to the submit button
var submitButton = this.getField("submitButton");
submitButton.setAction("MouseUp", "if (isAtLeastOneChecked()) { this.submitForm({}); } else { app.alert('Please select at least one checkbox.'); }");
Find more inspiration, events, and resources on the new Adobe Community
Explore Now