Copy link to clipboard
Copied
I have a form that has required fields and some javascript for required fields - for example "if NO is selected then make comments box required"
I'm using a submit by email button to submit the form... I also put in some script to lock the fields to make them read only on submit.
The problem I'm having is, if certain required fields are not complete when the submit button is clicked, the notification pops up to say some required fields are not complete, but it still locks the form so they cannot go back and add in the other information.
So I'm looking for a IF THAT THEN THIS scenario.
1. IF required fields are complete... THEN lock fields and submit the form
2. IF required fields are incomplete... THEN do not lock or submit ... allow more information to be added
Can anyone help me with button code?
I'm a Javascript newbie so please be gentle with me
thanks in advance
[Moved from non-technical Forum Lounge to specific Program forum... Mod]
[Here is the list of all Adobe forums... https://forums.adobe.com/welcome]
Copy link to clipboard
Copied
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){
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = true;
};
var to = "you@domain.com";
var strUrl = "mailto:"+to+"?subject=TEST&body=TEST&cc=&bcc=";
var submitAs = "PDF";
this.submitForm({cURL: strUrl, cSubmitAs:"PDF"});
};
Copy link to clipboard
Copied
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){
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = true;
};
var to = "you@domain.com";
var strUrl = "mailto:"+to+"?subject=TEST&body=TEST&cc=&bcc=";
var submitAs = "PDF";
this.submitForm({cURL: strUrl, cSubmitAs:"PDF"});
};
Copy link to clipboard
Copied
The above script cannot test the "listbox" filed since it cannot have the "required property" set to true. "buttons" do not have a required property. Also not all fields will have a null string as the default value. Two obvious fields are the "check box" and "radiobutton" fields which have a value of "Off" if not selected and may even have one member of the group "checked as default". "comboboxes" and "listboxes" usually have a non-selected value of " ", space. The only way to get a null string, "", is to populate the form using JavaScript. It is also possible to set a default value to most fields, look at the "Options" tab. I would look at testing a field's value to its default value. So there are a number of issues that might not be addressed by the above script. Since this is an action that might be needed by many different forms, I would create a function that could easily be incorporated into a form by adding a single page PDF with the function as a document level script function and then delete the blank page leaving behind the document level script.
A possible approach:
function CheckReq(aFields)
{
var aMissing = new Array();
var oField;
for(var i = 0; i < aFields.length; i++)
{
oField = this.getField(aFields);
if(oField.type != "button")
{
if(oField.value == oField.defaultValue)
{
aMissing.push(aFields);
} // end missed;
} // end not button or signature;
} // end field loop;
return aMissing;
} // end CheckingRez function;
// array of the names of reauired fields;
var TestFields = new Array("DateField", "List Box1", "Dropdown2", "Text3", "Text4");
// fill the variable with the names of the required fields not completed;
var aMissed = CheckReq(TestFields);
// test to see if there are any missing fields;
if(aMissed.length == 0)
{
app.alert("missed required fields!", 1, 0);
} else {
// loop through and lock all fields;
for(var i = 0; i < this.numFields; i++)
{
this.getField(this.getNthFieldName(i)).readonly = true;
}
// submit action;
}
Copy link to clipboard
Copied
//Updated version
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==f.defaultValue && f.type != 'button' && f.type != 'signature'){
ok=false;app.alert('Missing Value: '+f.name);
}
}
}catch(ex){};
};
if(ok==true){
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = true;
};
var to = "you@domain.com";
var strUrl = "mailto:"+to+"?subject=TEST&body=TEST&cc=&bcc=";
var submitAs = "PDF";
this.submitForm({cURL: strUrl, cSubmitAs:"PDF"});
};
Copy link to clipboard
Copied
Thanks that worked a treat...!
Just adding a more advanced question to the mix... Is there a way of submitting a locked form by email but keeping the local copy form unlocked?
For example this is for a form that must be filled out and submitted on a weekly basis. But if it's locked locally on the first submit I cannot then fill it out again next week as the fields are locked.
Copy link to clipboard
Copied
Method #1) You can insert a script after the form has been submitted to revert it to readonly=false
//Updated version
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==f.defaultValue && f.type != 'button' && f.type != 'signature'){
ok=false;app.alert('Missing Value: '+f.name);
}
}
}catch(ex){};
};
if(ok==true){
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = true;
};
var to = "you@domain.com";
var strUrl = "mailto:"+to+"?subject=TEST&body=TEST&cc=&bcc=";
var submitAs = "PDF";
this.submitForm({cURL: strUrl, cSubmitAs:"PDF"});
};
// Revert readonly back to false
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = false;
}
Method #2) If you have an ASP.net web server, you can host the PDF on your website and permanently flatten the form fields using iTextSharp, and then send the form using an SMTP account. The changes to the PDF will not be saved on the web server, and you can reuse the blank form.
See example #3 at the following website:
www.pdfemail.net/examples/
Copy link to clipboard
Copied
YES!!! You are awesome! Thanks
Is there anyway to insert {date} in the PDF filename that it attaches to the the email, or will it only save it as the original file name?
This is my last question I Promise.... you have been so super helpful!
Copy link to clipboard
Copied
You can only do that using a script that's installed on the user's local machine.
Copy link to clipboard
Copied
I thought that might be the case. Thanks so much for your reply.
Copy link to clipboard
Copied
The file name can't be altered using adobe's built-in JavaScript or mailto commands; but, the attachment can be re named to whatever you want using the server-side scripting method.
Copy link to clipboard
Copied
Not entirely true. Acrobat JS can be used to save the file under a new name (under certain circumstances, as I've said above), and then submit it using that new name.
Copy link to clipboard
Copied
Hi, I am using successfully your (Method #1) in reply no#5 above.
is there anything I can add to the end of the script that will reset the form?
so:
- fill out form
- submit form
- alerts if some required fields aren't complete
- lock form and submit by email
- unlock form still on desktop
- (new bit) clear form for re-submission
thanks in advanced
Copy link to clipboard
Copied
Yes, add the following javascript at the end: this.resetForm();
//Updated version
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==f.defaultValue && f.type != 'button' && f.type != 'signature'){
ok=false;app.alert('Missing Value: '+f.name);
}
}
}catch(ex){};
};
if(ok==true){
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = true;
};
var to = "you@domain.com";
var strUrl = "mailto:"+to+"?subject=TEST&body=TEST&cc=&bcc=";
var submitAs = "PDF";
this.submitForm({cURL: strUrl, cSubmitAs:"PDF"});
};
// Revert readonly back to false
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i));
f.readonly = false;
}
this.resetForm();
Copy link to clipboard
Copied
Hello,
I am just discovering the form in Acrobat x pro. It's easy to create a form from a model or from scratch but I need help to finalize the validation and Send form.
I would like to lock the form after it had been filled and sent. The idea is to not permit to the users to reuse the form after validation.
Is it possible ? Should I used the javascript ? If yes, how to add the code in the form ?
thanks in advance

