Skip to main content
Participating Frequently
February 23, 2022
Answered

How do I make the dropdown required by not allowing the "Please Select Option" as selected answer

  • February 23, 2022
  • 2 replies
  • 7151 views

Greetings!

May you please help me on how do I make the dropdown required and not allowing the "Please Select Option" as the selected option by the user.

Please see attached screenshot:

 

Thank you and hoping for your reply.

 

This topic has been closed for replies.
Correct answer Thom Parker

Set that field as required and then use this script as 'Validation' of that same field:

event.target.required = event.value == "Please Select Option" ? true : false;


A submit will be blocked when a field is both required and set to it's default value.  So make "Please Select" the default. I don't see any need for the script. 

 

 

 

 

2 replies

try67
Community Expert
Community Expert
February 23, 2022

You will need to validate the selection using a script, before sending the file.

Is this the only field you want to check, or are there more?

Participating Frequently
February 23, 2022

Hi try67!

 

May you please help me with the script?

 

try67
Community Expert
Community Expert
April 22, 2022

Hello ! I was looking for the same code and i've been looking into the forum to find the code but i can't seem to find it. Would it be possible for to provide it here ? Thank you so much !


Here's the generic function that will do that. It returns a boolean which can be used later on to decide whether to submit the file or not:

 

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) {
			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;
}	
Participating Frequently
February 23, 2022

Furthermore, this should not allow the user to submit the form unless it has met the requirement.

 

Thank You