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

Validate a drop down list with Javascript

New Here ,
Apr 04, 2022 Apr 04, 2022

I have a PDF form with a few drop down lists and I want to be able to validate that the person filling the form is not putting the default item. What is the javascript validation code I should be using ?

 

For example : One of my drop down list is Location and the user can choose between all of the locations we have. I added an option that is called « Select here » that is the default item in my drop down list. Is there a javascript code that would validate that the user didn't leave the « Select here » item on ?

TOPICS
Acrobat SDK and JavaScript , Windows
2.8K
Translate
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

correct answers 1 Correct answer

Community Expert , Apr 06, 2022 Apr 06, 2022

Here you go:

 

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;
}	

 

And then you can call it like this:

 

if (validateRequiredFi
...
Translate
Community Expert ,
Apr 05, 2022 Apr 05, 2022

What should trigger this validation?

Translate
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 ,
Apr 05, 2022 Apr 05, 2022

Also, what should happen if the default value is selected?

Translate
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
New Here ,
Apr 05, 2022 Apr 05, 2022

What should trigger is the selection of any other choices in the list.

 

If the default value is selected it should have an error message saying : Please choose a valid location (for example).

Translate
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 ,
Apr 05, 2022 Apr 05, 2022

Doing that will prevent the user from clearing the form... It's not a good idea.

Translate
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
New Here ,
Apr 05, 2022 Apr 05, 2022

So what should I do ? I need to make sure that the person filling the form chooses an option (a valid one) rather than leaving the default option. 

Translate
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 ,
Apr 06, 2022 Apr 06, 2022

Is the file being submitted back to you?

Translate
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
New Here ,
Apr 06, 2022 Apr 06, 2022

Yes, it will be submitted and sent back by email to the pay department.

Translate
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 ,
Apr 06, 2022 Apr 06, 2022

Then the place that makes the most sense to place the validation is on the Submit button.

Translate
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
New Here ,
Apr 06, 2022 Apr 06, 2022

Ok perfect, so what code should I put in order to do that validation on the submit button ?

Translate
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 ,
Apr 06, 2022 Apr 06, 2022

Do you want to validate only this one field?

Translate
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
New Here ,
Apr 06, 2022 Apr 06, 2022

No I have multiple dropdown lists so I want all of them to not be left with the default option

Translate
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 ,
Apr 06, 2022 Apr 06, 2022

I posted a script that allows you to do that several times on these forums. Search for "Validate Required Fields" and I'm sure you'll find it.

Translate
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
New Here ,
Apr 06, 2022 Apr 06, 2022

I did a lookup and I'm not quite sure which one would be the « complete » solution. There are so many answers and reply that I can't seem to find the « final » answer with the right code ?

Translate
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 ,
Apr 06, 2022 Apr 06, 2022

Here you go:

 

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;
}	

 

And then you can call it like this:

 

if (validateRequiredFields(this)) this.mailDoc({cTo: "me@server.com"});
Translate
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
New Here ,
Apr 07, 2022 Apr 07, 2022

Thank you so much !!

Translate
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
New Here ,
Nov 18, 2022 Nov 18, 2022

Hey! Thanks so much for this! Extremely helpful. I have a question though; when testing the form (distribute to myself, filling out out, and then submitting via the created button-with your script above), I noticed that once opening the distributed form, a purple bar appears at the top of the form that allows the user to "Submit Form" which completely side steps the JavaScript...any solution for this? (Like preventing that bar from appearing, or changing the code of the Submit Form button attached to it?)

Translate
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
New Here ,
Nov 18, 2022 Nov 18, 2022
LATEST

Or alternatively, adding to the script to not only email the fully completed pdf when my created button is clicked, but to also email an excel file (or csv) of the form responses? (I suspect this would get rid in the purple bar with the separate submit form button since it can be shared normally instead of through the "distribute" function...?)

Translate
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