Skip to main content
Participant
October 30, 2024
Question

PDF Form - dropdowns ignore mandatory fields and submits

  • October 30, 2024
  • 1 reply
  • 391 views

Can anyone help please, posted similar ask yesterday but I am new to custom scripts on PDF and have managed to work out some but need help on the following (have attached example form which was not attached to previous ask)

 

The form cannot submitted unless the mandatory fields below have an entry.  The submit button(s) will only show when a selection has been made in the Goods and /or Services dropdown

 

The problem I’m having is that form will not submit if the non-dropdown fields are not filled in (which is great), but it will submit if the dropdowns are not populated with an option 

 

Description 1

Total Amount 1

Total Amount Currency 1 (Dropdown)

Vendor

Email Address (custom script applied to ensure an email address is entered and not just a name)

Priority (Dropdown)

 

Also ideally want to add a custom script so that if the second line in description (Description 2) is filled in then the second line in the total amount currency section Total Amount 2 and Total Amount Currency 2 (Dropdown) then become mandatory and the form cannot be submitted if left blank and then also apply to 3rd / 4th and 5th following that rule 

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
October 30, 2024

You can use this code I wrote to achieve that:

 

if (validateRequiredFields(this)) this.mailDoc();

function validateRequiredFields(doc) {
	var emptyFields = [];
	for (var i=0; i<doc.numFields; i++) {
		 var f = doc.getField(doc.getNthFieldName(i));
		 if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
			if (typeof f.page=="object" && f.page.indexOf(-1)!=-1) continue;
			if (typeof f.page=="number" && f.page==-1) continue;
			if (f.display==display.hidden) continue;			 
			emptyFields.push(f.name);
		}
	}

	if (emptyFields.length>0) {
		 app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));
		 return false;
	}
	return true;
}	

 

To achieve your second request you can use this code as the custom Validation script of "Description 2":

this.getField("Total Amount 2").required = (event.value!=event.target.defaultValue);

this.getField("Total Amount 2 Currency").required = (event.value!=event.target.defaultValue);

 

PS. For the submit command you should use a button field, not a text field.

Participant
November 4, 2024

Hi try 67

I cannot get the if (validateRequiredFields(this)) this.mailDoc();  to work sorry but where do I put it, have tried in the dropdown fields but if I change any field the message that fields are not filled in appears on screen

 

Also on the submit command i used a text field as the field will move on the page based on the entry in the Goods and/or section (if Services is selected it hides the goods section)

 

As mentioned before am new to scripting