Skip to main content
Known Participant
July 5, 2018
Question

How to check string in java scripts for pdf forms

  • July 5, 2018
  • 19 replies
  • 5800 views

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 has been closed for replies.

19 replies

Known Participant
July 9, 2018

What I need?

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

Known Participant
July 9, 2018

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


It's work!

pixxxelschubser
Community Expert
Community Expert
July 9, 2018

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

Known Participant
July 9, 2018

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;

}

try67
Community Expert
Community Expert
July 9, 2018

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

Known Participant
July 9, 2018

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 .

Bernd Alheit
Community Expert
Community Expert
July 9, 2018

Use something like this:

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

try67
Community Expert
Community Expert
July 9, 2018

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.

Known Participant
July 9, 2018

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!

Bernd Alheit
Community Expert
Community Expert
July 9, 2018

Regex also works for:

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

Legend
July 5, 2018

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

pixxxelschubser
Community Expert
Community Expert
July 5, 2018

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

}

Known Participant
July 5, 2018

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

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

event.value = 100; 

else 

     event.value = 80; 

Doesnt work.

pixxxelschubser
Community Expert
Community Expert
July 5, 2018

Try

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

or

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

Known Participant
July 5, 2018

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

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

{

event.value = 100;

}

else

{

     event.value = 80;

}

No it doesn't work.

Known Participant
July 5, 2018

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.

try67
Community Expert
Community Expert
July 5, 2018

Change:

this.getField("fill_13").value

To:

event.value

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

BarlaeDC
Community Expert
Community Expert
July 5, 2018

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]