Skip to main content
AJ- monster
Inspiring
November 25, 2016
Answered

PDF Form - code to lock form fields on submit BUT NOT if *required fields are not complete

  • November 25, 2016
  • 2 replies
  • 8164 views

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]

This topic has been closed for replies.
Correct answer NKOWA555

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"});

};

2 replies

NKOWA555
Inspiring
November 28, 2016

//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"});

};

AJ- monster
Inspiring
November 28, 2016

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.

NKOWA555
Inspiring
November 28, 2016

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/

NKOWA555
NKOWA555Correct answer
Inspiring
November 27, 2016

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"});

};

Inspiring
November 27, 2016

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;              
}