Copy link to clipboard
Copied
I have a submit button on an Acrobat Form (originated from InDesign). The submit button currently has three different scrips on the same action that perform as follows :
SCRIPT 1
- Locks all fields
- hides 3 buttons
SCRIPT 2
- Verifies required fields are filled in correctly if true then
- attaches a pdf to an email with specified field data in the subject and body
- If false then returns an alert box for each missing field
SCRIPT 3
- unhides 3 buttons
- unlocks all fields
This allows the user to generate a copy of the form that does not look like a form for the end viewer. Currently, the button coding works.
I would like to improve the UI and consolidate my alert messages into one, but I can't seem to make it work. Below is the code for Script 2.
var ok = true;
var i = 0;
for (i = 0; i < this.numFields; i++){
try{
var f = this.getField(this.getNthFieldName(i));
if(f.required==true){
if(f.value==""){
ok=false;app.alert('Missing Value: '+f.name);
}
}
}catch(ex){};
};
if(ok==true){
// This is the form return email. It's hardcoded
// so that the form is always returned to the same address.
// Change address on your form to match the code below
var cToAddr = "user@email.com"
var Owner = this.getField("Owner").value;
var SerialNumber = this.getField("SerialComplete").value;
// Set the subject and body text for the email message
var cSubLine = "Report: " Owner + "
var cBody = "Report Attached. Serial Number: " + SerialNumber
// Send the entire PDF as a file attachment on an email
this.mailDoc({bUI: true, cTo: cToAddr, cSubject: cSubLine, cMsg: cBody});
};
Copy link to clipboard
Copied
Sorry, try this...
var ok = true;
var i = 0;
var flds = "";
for (i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
try{
if(f.required==true){
if(f.value==""){
ok=false;
if (flds == ""){
flds = flds + f.name;
}else{
flds = flds + "," + f.name;
}
};
};
}catch(e){
//app.alert(f.name + " " + e.description);
}
};
if(ok==true){
var cToAddr = "user@email.com";
var url = "mailto:" + cToAddr + "?subject=Report: " + encodeURIComponent(this.getField("Owner").value) + "&body=Report Attached. Serial Number: " + encodeURIComponent(this.getField("SerialComplete").value) + "&cc=&bcc=";
var submitAs = "PDF";//PDF,FDF,XFDF,XDP,XML
this.submitForm({cURL:url,cSubmitAs:submitAs});
}else{
app.alert('Missing Values: ' + flds);
};
Copy link to clipboard
Copied
There's not much you can do to change the built-in UI, and I don't see which messages you want to merge... Please provide more details.
Copy link to clipboard
Copied
Currently, for every field that is not filled in an Alert message pops up saying "Missing Value: [field name." This loops through something like 15 required fields. The end user can get an error message for every single missing field, having them push the ok button for each, just for another one to pop up. It's rather cumbersome.
I think what would work better, is in the if loop, to have an output field that you simple add the field to, then after it finished looping, if the output field is empty, no error message. If it contains information, to put up one error message with all the missing fields.
Something like the following... but I can't seem to get the code to work.
var output = "";
var ok = true;
var i = 0;
for (i = 0; i < this.numFields; i++){
try{
var f = this.getField(this.getNthFieldName(i));
if(f.required==true){
if(f.value==""){
ok=false;
output += f.name + ", ";
}
}
}
if output != ""{
app.alert('The following fields are required and must be filled in prior to submitting form: ' + output);
catch(ex){};
};
if(ok==true){
Copy link to clipboard
Copied
I recommend you remove the try-catch clauses, so you could see what's wrong with your code... Your approach is correct, though.
Copy link to clipboard
Copied
Sorry, try this...
var ok = true;
var i = 0;
var flds = "";
for (i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
try{
if(f.required==true){
if(f.value==""){
ok=false;
if (flds == ""){
flds = flds + f.name;
}else{
flds = flds + "," + f.name;
}
};
};
}catch(e){
//app.alert(f.name + " " + e.description);
}
};
if(ok==true){
var cToAddr = "user@email.com";
var url = "mailto:" + cToAddr + "?subject=Report: " + encodeURIComponent(this.getField("Owner").value) + "&body=Report Attached. Serial Number: " + encodeURIComponent(this.getField("SerialComplete").value) + "&cc=&bcc=";
var submitAs = "PDF";//PDF,FDF,XFDF,XDP,XML
this.submitForm({cURL:url,cSubmitAs:submitAs});
}else{
app.alert('Missing Values: ' + flds);
};
Copy link to clipboard
Copied
Try this:
var ok = true;
var i = 0;
for (i = 0; i < this.numFields; i++){
try{
var f = this.getField(this.getNthFieldName(i));
if(f.required==true){
if(f.value==""){
ok=false;app.alert('Missing Value: '+f.name);
}
}
}catch(ex){
app.alert("error(1):" + ex.description);
};
};
if(ok==true){
try{
// This is the form return email. It's hardcoded
// so that the form is always returned to the same address.
// Change address on your form to match the code below
var cToAddr = "user@email.com​";
//Place this script in the buttons mouseUp JavaScript event
//http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf#page=345
//SUBMIT PDF FORMAT WITH ADOBE READER
var url = "mailto:" + cToAddr + "?subject=Report: " + encodeURIComponent(this.getField("Owner").value) + "&body=Report Attached. Serial Number: " + encodeURIComponent(this.getField("SerialComplete").value) + "&cc=&bcc=";
var submitAs = "PDF";//PDF,FDF,XFDF,XDP,XML
this.submitForm({cURL:url,cSubmitAs:submitAs});
}catch(ex){
app.alert("error(2):" + ex.description);
};
};
Copy link to clipboard
Copied
How is that code any better? It will still show a separate error message for each field, which is what OP wanted to avoid...
Copy link to clipboard
Copied
This script put red strokes to all required fields that are empty, you can easily add an (only one) alert.
var emptyTest = /^\s*$/;
for(var i=0;i<this.numFields;i++) {
var nameField = this.getNthFieldName(i);
var fld = this.getField(nameField);
if(fld.type=="text") {
if(fld.required) {
if(emptyTest.test(fld.value)) {
fld.strokeColor = rougeCorporate;
}
}
}
}
And I also put this script in all required text fields, in the "On blur" event:
event.target.strokeColor = color.transparent;
Acrobate du PDF, InDesigner et Photoshopographe
Copy link to clipboard
Copied
Thanks for all your help. I've actually utilized both pieces of script from NKOWA555 and JR_Boulay. I'm hoping I could get one more piece of advice. The alert message is reporting correctly, and that's great.
However, I realize that my field names are not particularly user friendly. Some of them utilize parent fields for other functions, and many are without spaces. I thought if I could output the tooltip instead, this could be a way around it. I found "username" references the tooltip, but can't seem to make it work. The error message come back with "undefined." Any suggestions how to make that work, or any other ideas?
Here is the bit of code:
for (i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
try{
if(f.required==true){
if(f.value==""){
ok=false;
if (flds == ""){
flds = flds + f.username;
}else{
flds = flds + ", \n" + f.username;
}
};
};
}catch(e){
Copy link to clipboard
Copied
The name of this property is "userName", not "username". A small, but crucial, difference.
Copy link to clipboard
Copied
AH YES! Thank you!
Copy link to clipboard
Copied
Hello -
I am going through this thread and added the suggestions to my own form. The question I have on your response, is where do I put that code - in the JavaScript along with the other submit code: my current code detailed below?
I like the idea of having the missing required fields highlighted - so the form user can easily scroll through to the areas - as I think that is what your script is doing??
Sorry for such a newbie question, but this is my first form.
I appreciate any assistance.
Thank you
Keri
This is the code I have on my 'submit' form button:
var ok = true;
var i = 0;
var flds = "";
for (i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
try{
if(f.required==true){
if(f.value==""){
ok=false;
if (flds == ""){
flds = flds + f.name;
}else{
flds = flds + ", " + f.name;
}
};
};
}catch(e){
//app.alert(f.name + " " + e.description);
}
};
if(ok==true){
var cToAddr = "support@pennyandroseshop.com";
var cSubLine = "Credit Application "
var cCCAddr = this.getField("ClientEmail").value;
var cBody = "Thank you for submitting your form.\n" + "We will review your application and get back to you soon."
this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});
var submitAs = "PDF";//PDF,FDF,XFDF,XDP,XML
this.submitForm({cURL:url,cSubmitAs:submitAs});
}else{
app.alert('The following fields are required and must be filled in prior to submitting the credit application: ' + flds);
};
Copy link to clipboard
Copied
No, you should only use the script. Remove any other commands you added to the button.
Copy link to clipboard
Copied
Thank you for replying!
Sorry I was not clear, I am asking about the script from JR_Boulay: I am not sure how to incorporate that into my form, do I add the script below to the submit button, and if so not sure how to adjust my existing script (which is included in my previous reply) to include this additional code?
I appreciate any advice!
- var emptyTest = /^\s*$/;
- for(var i=0;i<this.numFields;i++) {
- var nameField = this.getNthFieldName(i);
- var fld = this.getField(nameField);
- if(fld.type=="text") {
- if(fld.required) {
- if(emptyTest.test(fld.value)) {
- fld.strokeColor = rougeCorporate;
- }
- }
- }
- }

