PDF form validation using javascript
Step-1- Go to Forms menu- Choose Add or Edit fields
Step-2- Double click to open the dialogue box of Properties of the selected Field.
Step-3- Tooltip- give the name here of the selected field and then * (for ex- Full Name*) then click on Close.
[NOTE- Here this is compulsory to write the ‘*’ after the field name]
This step will be done separately and repeated as same for all the fields in the form.
Step-4- Then select the button (Print button), right click on it.
Step-5- Go to Action Tab-
Delete the existing Action from the Actions, and
Add an Action-
· Select Trigger- Mouse up
· Select Action- Run a JavaScript (from the dropdown arrow)
· Then click on Add- (a dialogue box will be opened)
Write here - validateFields();
· Then click on OK.
· Then close this box.
Step-6- Now go to Advanced Menu- select the Document Processing- then select JavaScript Editor – firstly clear all the existing text or history.
and paste here- (paste the below text which is marked by yellow color)-
//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------
//
//validateFields
//
/*********** belongs to: Document-Level:validateFields ***********/
function validateFields()
{
//a counter for the number of empty fields
var flg = 0
// count all the form fields
var n = this.numFields
//create an array to contain the names of required fields
//if they are determined to be empty
var fArr = new Array();
//loop through all fields and check for those that are required
// all fields that have a '*' in their tool tip are required
for(var i = 0;i
var fn = this.getNthFieldName(i);
var f = this.getField(fn);
//tool tip is the fields\'s 'userName' property;
var tt = f.userName
//test for the '*';
if(tt.indexOf('*')!=-1 && f.value == f.defaultValue){
//increment the counter of empty fields;
flg++;
//add the fields userName (tool tip) to the list of empty field names;
fArr[fArr.length] = tt;
}
}
//now display a message if there are empty fields
if(flg>0){
app.alert('There are '+flg+' fields that require a value\n\n'+ fArr,3)
}
else{
this.print();
}
}
//
//
//
//Print:Annot1:MouseUp:Action1
//
/*********** belongs to: AcroForm:Print:Annot1:MouseUp:Action1 ***********/
validateFields();
//
//
Step-7-
After that again click on Advanced Menu- select the Document Processing- and then select Document JavaScript-
· Firstly, give the insert the name “Validate Fields” in the script name field.
· Click on Add – (a JavaScript editor dialogue box will be opened)
· then Clear all the existing history and then paste here (paste the below text which is marked by green color)-
{
//a counter for the number of empty fields
var flg = 0
// count all the form fields
var n = this.numFields
//create an array to contain the names of required fields
//if they are determined to be empty
var fArr = new Array();
//loop through all fields and check for those that are required
// all fields that have a '*' in their tool tip are required
for(var i = 0;i
var fn = this.getNthFieldName(i);
var f = this.getField(fn);
//tool tip is the fields\'s 'userName' property;
var tt = f.userName
//test for the '*';
if(tt.indexOf('*')!=-1 && f.value == f.defaultValue){
//increment the counter of empty fields;
flg++;
//add the fields userName (tool tip) to the list of empty field names;
fArr[fArr.length] = tt;
}
}
//now display a message if there are empty fields
if(flg>0){
app.alert('There are '+flg+' fields that require a value\n\n'+ fArr,3)
}
else{
this.print();
}
}
Then click on OK.
And then close this box.
Then go to Forms Menu and select the 2nd option “Close Form Editing”.
Then check it.
Comments