Skip to main content
Inspiring
February 19, 2024
Answered

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

  • February 19, 2024
  • 2 replies
  • 1438 views

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?

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

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. 

 

2 replies

try67
Community Expert
Community Expert
February 19, 2024

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.

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Ozkim24Author
Inspiring
February 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?

Thom Parker
Community Expert
Community Expert
February 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 PDFScriptingUse the Acrobat JavaScript Reference early and often