Copy link to clipboard
Copied
I have a PDF form with a few drop down lists and I want to be able to validate that the person filling the form is not putting the default item. What is the javascript validation code I should be using ?
For example : One of my drop down list is Location and the user can choose between all of the locations we have. I added an option that is called « Select here » that is the default item in my drop down list. Is there a javascript code that would validate that the user didn't leave the « Select here » item on ?
1 Correct answer
Here you go:
function validateRequiredFields(doc) {
var emptyFields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.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"));
return false;
}
return true;
}
And then you can call it like this:
if (validateRequiredFi
...
Copy link to clipboard
Copied
What should trigger this validation?
Copy link to clipboard
Copied
Also, what should happen if the default value is selected?
Copy link to clipboard
Copied
What should trigger is the selection of any other choices in the list.
If the default value is selected it should have an error message saying : Please choose a valid location (for example).
Copy link to clipboard
Copied
Doing that will prevent the user from clearing the form... It's not a good idea.
Copy link to clipboard
Copied
So what should I do ? I need to make sure that the person filling the form chooses an option (a valid one) rather than leaving the default option.
Copy link to clipboard
Copied
Is the file being submitted back to you?
Copy link to clipboard
Copied
Yes, it will be submitted and sent back by email to the pay department.
Copy link to clipboard
Copied
Then the place that makes the most sense to place the validation is on the Submit button.
Copy link to clipboard
Copied
Ok perfect, so what code should I put in order to do that validation on the submit button ?
Copy link to clipboard
Copied
Do you want to validate only this one field?
Copy link to clipboard
Copied
No I have multiple dropdown lists so I want all of them to not be left with the default option
Copy link to clipboard
Copied
I posted a script that allows you to do that several times on these forums. Search for "Validate Required Fields" and I'm sure you'll find it.
Copy link to clipboard
Copied
I did a lookup and I'm not quite sure which one would be the « complete » solution. There are so many answers and reply that I can't seem to find the « final » answer with the right code ?
Copy link to clipboard
Copied
Here you go:
function validateRequiredFields(doc) {
var emptyFields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.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"));
return false;
}
return true;
}
And then you can call it like this:
if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});
Copy link to clipboard
Copied
Thank you so much !!
Copy link to clipboard
Copied
Hey! Thanks so much for this! Extremely helpful. I have a question though; when testing the form (distribute to myself, filling out out, and then submitting via the created button-with your script above), I noticed that once opening the distributed form, a purple bar appears at the top of the form that allows the user to "Submit Form" which completely side steps the JavaScript...any solution for this? (Like preventing that bar from appearing, or changing the code of the Submit Form button attached to it?)
Copy link to clipboard
Copied
Or alternatively, adding to the script to not only email the fully completed pdf when my created button is clicked, but to also email an excel file (or csv) of the form responses? (I suspect this would get rid in the purple bar with the separate submit form button since it can be shared normally instead of through the "distribute" function...?)

