How to check string in java scripts for pdf forms

Community Beginner ,
Jul 05, 2018

Copy link to clipboard

Copied

Hello, my question is how to check one string in one field for example:
if in fill_20 the string is "Sofia" or SOFIA or sOfia or all combinations with lowercase letters and uppercase letters and so..
If fill_20 is with content=Sofia and all combination with lowercase and  uppercase letters then in fill_30 write value 100 if the string is different than Sofia then in fill_30 write 80?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

614

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to check string in java scripts for pdf forms

Community Beginner ,
Jul 05, 2018

Copy link to clipboard

Copied

Hello, my question is how to check one string in one field for example:
if in fill_20 the string is "Sofia" or SOFIA or sOfia or all combinations with lowercase letters and uppercase letters and so..
If fill_20 is with content=Sofia and all combination with lowercase and  uppercase letters then in fill_30 write value 100 if the string is different than Sofia then in fill_30 write 80?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

615

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 05, 2018 1
Adobe Community Professional ,
Jul 05, 2018

Copy link to clipboard

Copied

Hi,

The easiest way to manage this would be to use toLowerCase() or toUpperCase(), before your comparison, written out in fulll to make it easy to follow it would look something like :

var testString = this.getField("fill_20").valueAsString;

if ( testString.toUpperCase() == "SOFIA")

{

     this.getField("fill_30").value = 100;

}

else

{

     this.getField("fill_30").value = 30;

}

Hope this helps

Malcolm

[Edited to correct typo]

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Community Beginner ,
Jul 05, 2018

Copy link to clipboard

Copied

I tried this but nothing is happened.

var testString = this.getFIeld("fill_6").valueAsString; 

if ( testString.toUpperCase() == "SOFIA") 

     this.getField("fill_13").value = 100; 

else 

     this.getField("fill_13").value = 80; 

If I put SOFIA in fill_6 then write value 100 in fill_13

If in fill_6 the string is not SOFIA then write value 80 in fill_13.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Most Valuable Participant ,
Jul 05, 2018

Copy link to clipboard

Copied

Change:

this.getField("fill_13").value

To:

event.value

And make sure you place the code as the custom calculation script of "fill_13".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Community Beginner ,
Jul 05, 2018

Copy link to clipboard

Copied

var testString = this.getFIeld("fill_6").valueAsString; 

if ( testString.toUpperCase() == "София") 

event.value = 100; 

else 

     event.value = 80; 

Doesnt work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Most Valuable Participant ,
Jul 05, 2018

Copy link to clipboard

Copied

Try

if ( testString.toUpperCase() == "СОФИЯ") 

or

if ( testString.toLowerCase() == "софия")

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Community Beginner ,
Jul 05, 2018

Copy link to clipboard

Copied

var testString = this.getFIeld("fill_6").valueAsString;

if ( testString.toUpperCase() == "СОФИЯ")

{

event.value = 100;

}

else

{

     event.value = 80;

}

No it doesn't work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Most Valuable Participant ,
Jul 05, 2018

Copy link to clipboard

Copied

"Doesn't work" is not very helpful to us. You need to provide more details. Also, the fact that you changed the string to Cyrillic characters is quite crucial.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Most Valuable Participant ,
Jul 05, 2018

Copy link to clipboard

Copied

There is a subtle but important error in the first line. JavaScript is case sensitive, that is you must use capital or little letters exactly right. But you have a capital I in getFIeld.

This should be reasonably obvious from the error messages in the console, do you know how to check that?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 1
Most Valuable Participant ,
Jul 05, 2018

Copy link to clipboard

Copied

What does it say in the console, exactly, please?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 1
Most Valuable Participant ,
Jul 05, 2018

Copy link to clipboard

Copied

As Test Screen Name​ said:

The code of BarlaeDC​ has a typo in line #1

var testString = this.getFIeld("fill_6").valueAsString;

should be

var testString = this.getField("fill_6").valueAsString;

Furthermore:

Cyrillic characters shouldn't make any difference.

Try this code in ESTK (not in Acrobat)

var testString= "СоФиЯ";  //small and capital letters

if ( testString.toUpperCase() == "СОФИЯ")

{

alert("UpperCase works")

}

else

{

alert("UpperCase doesn't work")

}

if ( testString.toLowerCase() == "софия")

{

alert("LowerCase works")

}

else

{

alert("LowerCase doesn't work")

}

Are you sure that there is no space after ?

Here is another test (also for ESTK)

var testString= "СоФиЯ "; // small and capital letters + one space

if ( testString.toUpperCase() == "СОФИЯ")

{

alert("UpperCase works")

}

else

{

alert("UpperCase doesn't work")

}

if ( testString.toLowerCase() == "софия")

{

alert("LowerCase works")

}

else

{

alert("LowerCase doesn't work")

}

if ( testString.match(/софия/i) != null )

{

alert("Regex works")

}

else

{

alert("Regex doesn't work")

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 05, 2018 0
Community Beginner ,
Jul 08, 2018

Copy link to clipboard

Copied

Here everything is work:

  1. var testString= "СоФиЯ"//small and capital letters 
  2. if ( testString.toUpperCase() == "СОФИЯ"
  3. alert("UpperCase works"
  4. else 
  5. alert("UpperCase doesn't work"
  6. if ( testString.toLowerCase() == "софия"
  7. alert("LowerCase works"
  8. else 
  9. alert("LowerCase doesn't work"

The result is UpperCase works and LowerCase works.

Here :

  1. var testString= "СоФиЯ "; // small and capital letters + one space 
  2. if ( testString.toUpperCase() == "СОФИЯ"
  3. alert("UpperCase works"
  4. else 
  5. alert("UpperCase doesn't work"
  6. if ( testString.toLowerCase() == "софия"
  7. alert("LowerCase works"
  8. else 
  9. alert("LowerCase doesn't work"
  10.  
  11. if ( testString.match(/софия/i) != null
  12. alert("Regex works"
  13. else 
  14. alert("Regex doesn't work"

The result is UpperCase doesn't work, LowerCase doesn't work and Regex works!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 08, 2018 0
Adobe Community Professional ,
Jul 09, 2018

Copy link to clipboard

Copied

Regex also works for:

var testString= "СоФиЯ abc";

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 09, 2018 0
Adobe Community Professional ,
Jul 09, 2018

Copy link to clipboard

Copied

HI,

It looks like Uppercase and Lowercase if statements are working as expected, as you have added a space, the strings do not match, and therefore it would always be the else that is fired.

The regex is checking that the string contains the characters and therefore it is matching, as it should.

If you want to include the space you need to include that in the IF statements as well. something like

var testString= "СоФиЯ "; // small and capital letters + one space

if ( testString.toUpperCase() == "СОФИЯ ")

{

alert("UpperCase works")

}

else

{

alert("UpperCase doesn't work")

}

if ( testString.toLowerCase() == "софия ")

{

alert("LowerCase works")

}

else

{

alert("LowerCase doesn't work")

}

if ( testString.match(/софия/i) != null )

{

alert("Regex works")

}

else

{

alert("Regex doesn't work")

Or you could use a function or similar to remove any whitespace from your string before you start something like this would work.

function trim (aString)

{

    return aString.replace(/(^\s*)|(\s*$)/g,"");

}

this removes whitespace from the string, allowing you to compare just the characters.

Hope this helps

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 09, 2018 0