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

Find blank field in a group of fields

Explorer ,
May 10, 2023 May 10, 2023

I have a document with 10 sections. Each section has 10 fields , section 1 (jt1.01 to jt1.10), section 2 (jt2.01 to jt2.10), and so on.

Could someone please help me for adobe javascript code to find any empty field (all are text fields) in a section and alert me to fill the blank field(s) before signing the section and make all fields readonly.

 

Thanks in advance

TOPICS
General troubleshooting , PDF forms
3.0K
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
4 ACCEPTED SOLUTIONS
Community Expert ,
May 10, 2023 May 10, 2023

This will list all empty fields:

var fields = [];
for(var i=1; i<=10; i++){
var f = this.getField("jt"+i).getArray();
for( var j in f){
if(f[j].valueAsString == "")
fields.push(f[j].name);}}
if(fields.length != 0)
app.alert("Please fill in blank fields:\n"+fields.join("\n"));

View solution in original post

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
Explorer ,
May 11, 2023 May 11, 2023

Thanks so much for a quick response.

It worked like a charm. The only thing I had to change was change "jt" to "jt.".

I created a Button "jt-click" and placed the code in "Actions", "Mouse Up", "Run a Javascript". The fields are named jt.1 to jt.10, When "jt-click" is pushed, a list of blank fields is displayed.

What additional code I need to add to "jt-click" Button in order to make it disappear if all 10 text fields are filled.

Thanks in advance again.

View solution in original post

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 ,
Jun 02, 2023 Jun 02, 2023

You can apply it to all (text) fields using this code:

 

for (var i=0; i<this.numFields; i++) {
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname);
	if (f==null) continue;
	if (f.type=="text") f.setAction("Keystroke", "spacebar();");
}

 

Note this will overwrite any previous Keystroke actions (and also Format, in some cases) you selected, though.

View solution in original post

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
Explorer ,
Jun 02, 2023 Jun 02, 2023
LATEST

Thanks try67,

WOW - I am impressed with the level of experties of the Adobe Support Community Experts and their responsiveness.

Your code worked perfectly. 

I modified your code for a selected section as an array (rather than the entire document) as Ms. Nurani advised earlier [ for( var j in f){
if (f[j]==null) continue; if (f[j].type=="text") f[j].setAction("Keystroke", "spacebar();");}].

I am really greatful for your help. 

Thanks again.

 

 

View solution in original post

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 ,
May 10, 2023 May 10, 2023

This will list all empty fields:

var fields = [];
for(var i=1; i<=10; i++){
var f = this.getField("jt"+i).getArray();
for( var j in f){
if(f[j].valueAsString == "")
fields.push(f[j].name);}}
if(fields.length != 0)
app.alert("Please fill in blank fields:\n"+fields.join("\n"));
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
Explorer ,
May 11, 2023 May 11, 2023

Thanks so much for a quick response.

It worked like a charm. The only thing I had to change was change "jt" to "jt.".

I created a Button "jt-click" and placed the code in "Actions", "Mouse Up", "Run a Javascript". The fields are named jt.1 to jt.10, When "jt-click" is pushed, a list of blank fields is displayed.

What additional code I need to add to "jt-click" Button in order to make it disappear if all 10 text fields are filled.

Thanks in advance again.

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 ,
May 11, 2023 May 11, 2023

In your first post, you wrote that you had 10 sections with 10 fields in each, named jt1.01-jt1.10, jt2.01-jt2.10...etc.

Now you wrote fields are named jt.1-jt.10, does that mean you only have 10 fields?

If your fields are named as you stated in the first post, and you changed to "jt." script won't work.

 

What additional code I need to add to "jt-click" Button in order to make it disappear if all 10 text fields are filled.

If all fields are filled alert won't pop.

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
Explorer ,
May 11, 2023 May 11, 2023

You are correct. I just changed field names to simplify the testing process. All is working now. Thanks again for your expert advice and input.

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
Explorer ,
Jun 01, 2023 Jun 01, 2023

Ms. Nurani,

Thanks for solving my first problem regarding blank fields.

How could I modify the above codes not to allow space bar as first character for the blank fields. I do not want to use custom Keystroke for each field.

var v= AFMergeChange (event);

if (/^\s/.test(v)) event.rc=false;

Thanks in advance again.

 

 

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 ,
Jun 01, 2023 Jun 01, 2023

Put the code in a doc-level function and then call it from each 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
Explorer ,
Jun 01, 2023 Jun 01, 2023

Thanks TRY67,

I am a good chemist but a novice Adobe JavaScript programmer. I create interactive forms using Adobe Acrobat 10 Pro for some of the work I do. 

How do I put the code in a doc level in Acrobat 10 Pro and how do I call it from each field?

 

Thanks in advance.

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
Explorer ,
Jun 01, 2023 Jun 01, 2023

Hi try67,

I found one of your posts about doc-level scripts.

1- Created a function called "spacebar" at doc-level

function spacebar()
{var v = AFMergeChange(event);
if (/^\s/.test(v)) event.rc = false;
}

2- Then tried to get the "spacebar ();" funtion at the field level by entering, 

spacebar ();

in the test field properties "Calculate" - Custom calculation script. 

 

Still I can enter space bar in the field. How do I call the "spacebar ();" function in the field?

Thanks

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 ,
Jun 01, 2023 Jun 01, 2023

Not in calculation, go to 'Format' tab and select 'Custom' and put spacebar(); in 'Custom keystroke script'.

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
Explorer ,
Jun 02, 2023 Jun 02, 2023

Thanks Ms. Nuurani,

That worked perfect.

However, I was hoping to have the codes in a separate field that when it checks for empty fields in a section also dis-allow spacebar entry as first character for all fields in that section. Right now, I need to go to each individual Text Field Properties,, select Format, Custom, Custom Keystroke Script - and enter the "spacebar ();". It is time consuming to do this for about 100 fields and also adds to the size of the file. I was hoping for a simpler way.

Thanks again in advance

 

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 ,
Jun 02, 2023 Jun 02, 2023

What is the naming of the fields?

You can use loop and setAction() to put script in all fields at once.

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 ,
Jun 02, 2023 Jun 02, 2023

You can apply it to all (text) fields using this code:

 

for (var i=0; i<this.numFields; i++) {
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname);
	if (f==null) continue;
	if (f.type=="text") f.setAction("Keystroke", "spacebar();");
}

 

Note this will overwrite any previous Keystroke actions (and also Format, in some cases) you selected, though.

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
Explorer ,
Jun 02, 2023 Jun 02, 2023
LATEST

Thanks try67,

WOW - I am impressed with the level of experties of the Adobe Support Community Experts and their responsiveness.

Your code worked perfectly. 

I modified your code for a selected section as an array (rather than the entire document) as Ms. Nurani advised earlier [ for( var j in f){
if (f[j]==null) continue; if (f[j].type=="text") f[j].setAction("Keystroke", "spacebar();");}].

I am really greatful for your help. 

Thanks again.

 

 

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