• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

PDF Form - dropdowns ignore mandatory fields and submits

Community Beginner ,
Oct 30, 2024 Oct 30, 2024

Copy link to clipboard

Copied

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 

TOPICS
PDF forms

Views

149

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 30, 2024 Oct 30, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 04, 2024 Nov 04, 2024

Copy link to clipboard

Copied

LATEST

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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines