Skip to main content
Known Participant
Jul 5, 2018 6:07 PM UTC
Question

How to check string in java scripts for pdf forms

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?

This topic is closed to new replies. Start a new post to keep the conversation going.

39 replies

Known Participant
Jul 9, 2018 1:07 PM UTC

What I need?

I tested all combination with София  and it's work?

Translate
Known Participant
Jul 9, 2018 12:50 PM UTC

I write this script in fill_13 where is the fill where I want to put value 100 or 80.


It's work!

Translate
pixxxelschubser
Community Expert
Community Expert
Jul 9, 2018 1:04 PM UTC

You don't need all three if clauses. Finally you only need one of them.

Translate
Known Participant
Jul 9, 2018 12:49 PM UTC

Ieeeeeee!

It's work!

Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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

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

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

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

{

event.value = 100;

}

else

{

event.value = 80;

}

Translate
try67
Community Expert
Community Expert
Jul 9, 2018 12:58 PM UTC

Your conditions are redundant. If the first condition is true then the other two will be true by definition. They are not needed.

Translate
Known Participant
Jul 9, 2018 9:29 AM UTC

Thank you but how can I implement it with all combinations of София with upper and lower case and space.
If София is in fill_20  then in fill 30 write 100 else write 80 .

Translate
Bernd Alheit
Community Expert
Community Expert
Jul 9, 2018 9:47 AM UTC

Use something like this:

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

Translate
try67
Community Expert
Community Expert
Jul 9, 2018 10:25 AM UTC

Note that the trim method is not supported in versions of Acrobat prior to DC. It's better to define your own function, just in case.

Translate
Known Participant
Jul 9, 2018 6:43 AM UTC

Here everything is work:

  1. var testString= "СоФиЯ";  //small and capital letters 
  2. if ( testString.toUpperCase() == "СОФИЯ") 
  3. { 
  4. alert("UpperCase works") 
  5. } 
  6. else 
  7. { 
  8. alert("UpperCase doesn't work") 
  9. } 
  10. if ( testString.toLowerCase() == "софия") 
  11. { 
  12. alert("LowerCase works") 
  13. } 
  14. else 
  15. { 
  16. alert("LowerCase doesn't work") 
  17. } 

The result is UpperCase works and LowerCase works.

Here :

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

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

Translate
Bernd Alheit
Community Expert
Community Expert
Jul 9, 2018 7:40 AM UTC

Regex also works for:

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

Translate
Legend
Jul 5, 2018 9:02 PM UTC

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

Translate
pixxxelschubser
Community Expert
Community Expert
Jul 5, 2018 10:21 PM UTC

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")

}

Translate
Known Participant
Jul 5, 2018 7:48 PM UTC

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

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

{ 

event.value = 100; 

} 

else 

{ 

     event.value = 80; 

} 

Doesnt work.

Translate
pixxxelschubser
Community Expert
Community Expert
Jul 5, 2018 7:54 PM UTC

Try

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

or

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

Translate
Known Participant
Jul 5, 2018 8:05 PM UTC

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

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

{

event.value = 100;

}

else

{

     event.value = 80;

}

No it doesn't work.

Translate
Known Participant
Jul 5, 2018 7:35 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Jul 5, 2018 7:40 PM UTC

Change:

this.getField("fill_13").value

To:

event.value

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

Translate
BarlaeDC
Community Expert
Community Expert
Jul 5, 2018 6:55 PM UTC

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]

Translate