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

script for making a field Required based on a previous fields input

Participant ,
Nov 18, 2021 Nov 18, 2021

Hi all,

 

Is is possible to have a script to make one field 'Required' when another field is filled in?

 

I have a form that has a page of 36 fields for user input. I have three columns (Date, Details, Initials) and 12 rows (first row: Date1, Details1 and Initials1, second row: Date2, Details2 and Initials2... and so on - you get the picture). I need the fields named 'Initials' to be 'Required' but only if the fields in it's row have been filled out. For instance if someone puts information in 'Details1' I would like 'Initials1' to be automatically set to 'Required'. I can't set all 12 'Initial' fields to 'Required' as all would need an entry reguardless if the corresponding 'Details' field has any information and not all rows will be filled in at the same time, hence the 'Date' field. Someone may come back a week or 2 later to enter more information and when they update the nest 'Details' field the corresponding 'Initials' field would be set to 'Required' at that time.

 

I hope I explained this so you can understand it and that someone can help me out.

 

Thanks
Paul

TOPICS
Create PDFs , How to , PDF forms
4.4K
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
1 ACCEPTED SOLUTION
Participant ,
Nov 24, 2021 Nov 24, 2021

Thanks Thom,

 

You know I had a much longer response that I was going to include but I feel it was only going to get into a back and forth. So thanks for the effort to help but I ended up going a different route.

 

I eventually got the end result I wanted, maybe not the most elegant but results is what counted. I am going to include the script incase others, like myself, need it. But ask me for changes and you are out of luck. 🙂

 

var dt = this.getField("DATE1").value;
if(dt!="")
{
this.getField("INITIALS1").required = true;
}else{
this.getField("INITIALS1").required = false;
}

 

Thanks

Paul

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 ,
Nov 18, 2021 Nov 18, 2021

Yes this can be done. However, the difficulty is related to the field naming.  Fortunately it looks like your names follow a consistent pattern. This is very important.  

 

One way to do this is to use a calculation script. Put this script in the custom calculatin on literally any field in the document, except one of the Initials field.  Usually I'd use a hidden field just for this purpose. 

 

 

 

if(/Date(\d+)/.test(event.source.name) || /Details(\d+)/.test(event.source.name))
{
   var Row = RegExp.$1;
   this.getField("Initials" + Row).required = ((this.getFiel("Date" + Row).value != "") || (this.getFiel("Details" + Row).value != ""));
}

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Participant ,
Nov 18, 2021 Nov 18, 2021

Hi Thom,

Thanks for the quick reply. I created a separate field and made it hidden and add your script to the custom calc but I am getting a

 

SyntaxError: missing ) in parenthetical

4: at line 5

 

123WorkingGuy_1-1637272902352.pngexpand image

Any thoughts?

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
Community Expert ,
Nov 18, 2021 Nov 18, 2021

I fixed the code in the previous post.  there was a ending parentheses missing at the end of the 2nd to last line.

 

On that note. I did not test this code. I would have to create a test form to do that, which I'm not going to do.  The code is conceptually correct, but there may be some details (such as the one you reported)  that need fixing. This is standard stuff for scripting and if you're going to be using scripts, then at a minimum you should be able to fix things like balancing brackets and parentheses.  

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Participant ,
Nov 24, 2021 Nov 24, 2021

Thanks Thom,

 

You know I had a much longer response that I was going to include but I feel it was only going to get into a back and forth. So thanks for the effort to help but I ended up going a different route.

 

I eventually got the end result I wanted, maybe not the most elegant but results is what counted. I am going to include the script incase others, like myself, need it. But ask me for changes and you are out of luck. 🙂

 

var dt = this.getField("DATE1").value;
if(dt!="")
{
this.getField("INITIALS1").required = true;
}else{
this.getField("INITIALS1").required = false;
}

 

Thanks

Paul

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
Participant ,
Nov 24, 2021 Nov 24, 2021
LATEST

So I guess I jumped the gun a bit here. The script does work in the sense it makes my one field 'Required' once the date is filled in and when you go to save the file without that 'Required' field filled in it goes straight to 'Save As'.

 

But I was hoping that Adobe would have a dialogue box pop up that says you need to fill in this required field before saving? Or is this going to require another script somewhere?

 

Thanks
Paul

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