Copy link to clipboard
Copied
Hello,
I've created a form in which certain check boxes and fields are required. When clicking on the submit button, I would like it to verify if those check boxes and fields have been filled in. If they have, then send a mail with the PDF. However, if they haven't, then highlight the empty fields.
Unfortunately, I know pretty much nothing of JavaScript, which means that if I find an article that shows the solution, it doesn't work for me (probably because I didn't specify the names of the fields...). Can you please help me out?
The check boxes that need to be filled in are "Customer" and "Site". The fields that are required are "Name" and "Contact".
Highlighting the fields is a bit tricky. Try this code first:
...if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});
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 th
Copy link to clipboard
Copied
Highlighting the fields is a bit tricky. Try this code first:
if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});
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;
}
Copy link to clipboard
Copied
It doesn't have to highlight, it can just be a red box or a pop-up or basically just something that indicates that values are missing.
Unfortunately, that didn't work. Nothing happens if I leave the fields blank or if I fill them in (at which point I should be receiving a mail I guess).
I changed the email address, changed the name of the button and changed the name of the field. First I tried by using the names of 2 different fields and then just the one "Name" field.
Copy link to clipboard
Copied
Don't change anything in the code I provided, especially if you don't know what you're doing...
Copy link to clipboard
Copied
Except for the email address to send the file to, of course...
Copy link to clipboard
Copied
Oh ok! So I've tried it again without making changes (except the email address) and when I click on Submit it opens up the mail with the PDF, no matter if the fields are filled or not.
How can I specify which fields it should look at?
Copy link to clipboard
Copied
You have to set them as Required (right-click them in Form Edit mode, go to Properties and you'll see it under the General tab).
Copy link to clipboard
Copied
Thank you. I see that my question may have been wrongly formulated. I was hoping to be able to have that solution without having to mark the fields as required. I don't like how the fields are straight away marked in red when they're marked as required (even before people start filling in the form). The person filling in the form should only be notified if they've left a specific field blank.
Can that be done?
Copy link to clipboard
Copied
Yes, but then you will need to hard-code their names in the code.
Use this:
if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});
function validateRequiredFields(doc) {
var requiredFields = ["Customer", "Site", "Name", "Contact"];
var emptyFields = [];
for (var i=0; i<requiredFields.length; i++) {
var f = doc.getField(requiredFields);
if (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;
}
Copy link to clipboard
Copied
YES!!!!!! YOU ARE A GENIOUS!!!!! THANK YOU VERY MUCH!