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

Javascript to prevent form submit/warn if two fields are identical

Explorer ,
Feb 19, 2024 Feb 19, 2024

On a mac using Acrobat Pro. I'm working on a pdf form with a fair amount of embedded javascript and I'm stuck on something that seems like it should be straightforward, but my script knowledge is just too basic.

There are two fields which need to be unique. If the same number goes in both fields I'd like a warning to flash up and to prevent the pdf from being submitted. I tried adding a validation script to the individual fields. Field A:
if (this.getField("FieldB").value==event.value({
app alert("Warning");
}

I've tried variants on this defining the variable of fields A and B  and doing if/else but I'm just not managing to get it to work. 

The two fields which need to be unique have a custom format as they are number fields which allow leading zeros, not sure if this is complicating matters? They're both required fields.

Anyone able to point me in the right direction?

TOPICS
JavaScript , PDF , PDF forms
875
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
Community Expert ,
Feb 19, 2024 Feb 19, 2024

Your custom validation script look good, but it has a couple of syntax errors.

 

if (this.getField("FieldB").value==event.value){
     app.alert("Warning");
}

 

See if this works. 

 

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

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 ,
Feb 19, 2024 Feb 19, 2024

Your custom validation script look good, but it has a couple of syntax errors.

 

if (this.getField("FieldB").value==event.value){
     app.alert("Warning");
}

 

See if this works. 

 

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
Explorer ,
Feb 19, 2024 Feb 19, 2024

Thanks so much for response - the field 'accepts' your code but I'm not getting a warning pop-up when the fields are identical. I've added it to the field as a Validation script - is that correct?

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 ,
Feb 19, 2024 Feb 19, 2024

Yes, it needs to be a validation script. There's nothing obvious, so you'll need to do some debug now.  Start by looking in the console window for any reported errors. Then add a console.println statement into the validation script so you can see exactly what the values are that are being used. 

 

BTW: the number formatting for the fields has no affect on the script, or the real field value.  But it could be making it look as if the fields are the same, when they are not.  For testing this script I would suggest removing the format so you can see the real values. 

 

 

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
Explorer ,
Feb 19, 2024 Feb 19, 2024

I'm not sure what I did - nothing came up in debugger so I removed the custom format script. Warning worked. Put the custom format script back in and it's all still working! Which is the main thing. Thank you so much I've been going daft trying to figure this out! 

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 ,
Feb 19, 2024 Feb 19, 2024
LATEST

To prevent the file from being submitted you will need to submit it using a script (not the built-in Submit a Form command), and add this logic before the command to send the file.

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