Copy link to clipboard
Copied
Hello. I have a form which includes text fields (ID number, date of birth), quite a few radio buttons for numerous questionnaires (some are yes, no, or unsure questions while others are numerical scales ), and some dropdown lists, almost all of which are required fields. I have a button at the end of the document which auto saves the form for me.
I would also like this button to check if all the required fields are completed, and report back any that aren't in an alert. I have found the following Javascript code (from here Re: Check to see if any field is empty) which partly works.
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required ) {
if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Please complete the following fields:\n" + emptyFields.join("\n"));
}
It reports back accurately on the text fields, however it doesn't work for the radio buttons or dropdowns. I tried adding the script || (f.type=="radios" && f.value=="Off") after the checkbox part, which resulted in all the radio buttons being listed in the alert even if they were filled. Perhaps this is because, by the nature of radio buttons, you can only select one option so some buttons are always left unchecked. I'm not particularly familiar with JavaScript so I am having trouble trying to solve it myself. Any help would be appreciated. I'm using Acrobat XI Pro. Thanks.
Change this line:
if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);
To:
if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);
Copy link to clipboard
Copied
Change this line:
if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);
To:
if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);
Copy link to clipboard
Copied
Thank you, that works perfectly
Copy link to clipboard
Copied
I was having the same problem and this worked for me too. Thanks for the help
Copy link to clipboard
Copied
I put this code in the document javascripts to make the submit by email button to run.
function Ex2ValidFields()
{
var bRtn = false;
var aErrMsg = [];
var rgEmpty = /^\s*$/;
if(rgEmpty.test(this.getField("code No").value))
aErrMsg.push("CODE NO. IS REQUIRED");
if(aErrMsg.length == 0)
bRtn = true;
else
app.alert("Line Check\nOne or more required fields have not been filled out:\n\n * " + aErrMsg.join("\n * "));;
return bRtn;
}
///////////////////
now i want to merg this code:
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required ) {
if (f.valueAsString==f.defaultValue)aErrMsg.push(f.name);
}
to the first code to read the radio buttons as a required fields
can you help please ?
thank you
Copy link to clipboard
Copied
Do you just want to have a special message for that one field?
Copy link to clipboard
Copied
This worked perfectly for all fields except my conditionally required text fields. For several radio button questions, I have indluded code which changes a subsequent text field to required for any "Yes" radio buttons selected. The global validation doesn't seem to alert to these empty fields. It works for all other fields, including a text field that doesn't have any conditions applied. Is there additional code I can insert to address these conditionally required fields? Thanks so much in advance!
Copy link to clipboard
Copied
FYI, I have this code applied to my digital signature field:
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required ) {
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"));
}
Copy link to clipboard
Copied
Is there a way to add a line that if no errors are found, the warning box says "No errors found"?
Copy link to clipboard
Copied
Add this as the last line of code:
else app.alert("No errors found.",3);
Copy link to clipboard
Copied
I want to Show some message is ther is no empty fields. I tried adding
else app.alert("No errors found.",3); but shows no errors found even there is empty field.
can you help please
var requiredFields = new Array(24);
requiredFields[0] = "Aadhaar Number";
requiredFields[1] = "AplVal";
requiredFields[2] = "Applicant Name Eng";
requiredFields[3] = "Applicant Name Tam";
requiredFields[4] = "GenVal";
requiredFields[5] = "MarVal";
requiredFields[6] = "Date of Birth_af_date";
requiredFields[7] = "RelVal";
requiredFields[8] = "Father Name Eng";
requiredFields[9] = "Father Name Tam";
requiredFields[10] = "Mother Name Eng";
requiredFields[11] = "Mother Name Tam";
requiredFields[12] = "RgnVal";
requiredFields[13] = "ComVal";
requiredFields[14] = "OcuVal";
requiredFields[15] = "DisVal";
requiredFields[16] = "TalVal";
requiredFields[17] = "Street Name Eng";
requiredFields[18] = "Street Name Tam";
requiredFields[19] = "Door Number";
requiredFields[20] = "Pin Code";
requiredFields[21] = "PinVal";
requiredFields[22] = "Mobile Number";
requiredFields[23] = "MobVal";
// These are the alert messages shown when a required field is empty.// Populate array with messages.// Make sure there's one message for each required field.
var alertMsg = new Array(24);
alertMsg[0] = "Please enter Aadhaar Number.";
alertMsg[1] = "Please Select Appelation.";
alertMsg[2] = "Please enter Applicant Name in English.";
alertMsg[3] = "Please enter Applicant Name in Tamil.";
alertMsg[4] = "Please Select Gender.";
alertMsg[5] = "Please Select Marital status.";
alertMsg[6] = "Please enter Date of birth.";
alertMsg[7] = "Please Select Relationship.";
alertMsg[8] = "Please enter Father name in English.";
alertMsg[9] = "Please enter Father name in Tamil.";
alertMsg[10] = "Please enter Mother name in English.";
alertMsg[11] = "Please enter Mother name in Tamil.";
alertMsg[12] = "Please Select Religion.";
alertMsg[13] = "Please Select Community.";
alertMsg[14] = "Please Select Occupation.";
alertMsg[15] = "Please Select District.";
alertMsg[16] = "Please Select Taluka.";
alertMsg[17] = "Please enter Street name in English.";
alertMsg[18] = "Please enter Street name in Tamil.";
alertMsg[19] = "Please enter Door Number.";
alertMsg[20] = "Please enter Pincode.";
alertMsg[21] = "Pincode must be 6 digit.";
alertMsg[22] = "Please enter Mobile Number.";
alertMsg[23] = "Mobile Number must be 10 digit.";
var emptyTest = /^\s*$/;
var fieldCount = requiredFields.length
var fld = 0;
for (var i=0; i < fieldCount; i++)
{fld = this.getField(requiredFields[i]);
if( emptyTest.test(fld.value) )
{
app.alert(alertMsg[i]);fld.setFocus();break;
}
else app.alert("No errors found.",3);
}
Copy link to clipboard
Copied
You want to show one error message at a time? Wouldn't it be better to collect them all and show them all at once?
Copy link to clipboard
Copied
yes it would be better if able show all errors at once, i dont know how to do that.
In my form i have text box and dropdown list only. I'm indirectly validating Dropdowns using hidden text box so i cannot use required fields method. can you please suggest me the appropriate validation method form my form, I had attached my form. Required fields were filled
Copy link to clipboard
Copied
Change the last part of the code to this:
var errorMsgs = [];
var firstEmptyField = null;
for (var i=0; i < fieldCount; i++) {
var fld = this.getField(requiredFields[i]);
if ( emptyTest.test(fld.value) ) {
errorMsgs.push(alertMsg[i]);
if (firstEmptyField==null) firstEmptyField = fld;
}
}
if (errorMsgs.length==0) app.alert("No errors found.",3);
else {
firstEmptyField.setFocus();
app.alert(errorMsgs.join("\n"));
}
Copy link to clipboard
Copied
Wow thankyou so much sir for Quick and super solution, you are doing Great!!
I had learned lot of scripts form your posts. Thankyou once again
Copy link to clipboard
Copied
I have tried the code below to check 2 required fields, on clicking button nothing happends, have tried clearing form, any help would be greatly appreciated on this one :
var requiredFields = new Array(2);
requiredFields[0] = "company_name_print";
requiredFields[1] = "nature_of_business";
var alertMsg = new Array(2);
alertMsg[0] = "Please enter Company Name.";
alertMsg[1] = "Please enter Nature of Business.";
var errorMsgs = [];
var firstEmptyField = null;
for (var i=0; i < fieldCount; i++) {
var fld = this.getField(requiredFields[i]);
if ( emptyTest.test(fld.value) ) {
errorMsgs.push(alertMsg[i]);
if (firstEmptyField==null) firstEmptyField = fld;
}
}
if (errorMsgs.length==0) app.alert("No errors found.",3);
else {
firstEmptyField.setFocus();
app.alert(errorMsgs.join("\n"));
}
Copy link to clipboard
Copied
fieldCount is not defined.
Check the Javascript console for errors.
Copy link to clipboard
Copied
I am getting an error that the fld variable is null, any idea why that might be? Thanks in advance.
Copy link to clipboard
Copied
How does you set the variable?
Copy link to clipboard
Copied
"yes it would be better if able show all errors at once"
You are going to drive your users crazy!
I use to turn all empty required fields with a red stroke, and before I added this script in action "onFocus" to all of them:
event.target.strokeColor = color.transparent;
(transparent or the previous color if any)