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

Check if two fields have same value

Explorer ,
May 21, 2018 May 21, 2018

I am trying to write a script that verifies that fieldA and fieldB do not contain identical strings.  And alerting the user if they are the same.  I have the following script (located in the validation of fieldB, but when I enter the same text in both boxes, no alert appears.

if(this.getfield("fieldA".value) == event.value) {

  app.alert("Must be different than fieldA");

}

TOPICS
Acrobat SDK and JavaScript , Windows
1.1K
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 21, 2018 May 21, 2018

You have errors in your code. Check the JS Console.

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
LEGEND ,
May 21, 2018 May 21, 2018

When comparing field values like it seems you want, you should use the valueAsString field property, so the code could look like:

if (this.getField("fieldA").valueAsString == event.value) {

  app.alert("Must be different than fieldA");

}

Note that event.value will always be a string in this context.

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
LEGEND ,
May 21, 2018 May 21, 2018

JavaScript is case sensitive for everything.

What should happen if both fields are empty?

Your script should allow for the fields to have their values be changed to another value or even to no value.

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 23, 2018 May 23, 2018
LATEST

Thank you all.  I found the errors in my script.  The check works with .value or .valueAsString.  I added a qualifier to not perform the check if fields left blank.

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