How to check string in java scripts for pdf forms

Community Beginner ,
Jul 05, 2018 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

629

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
community guidelines
Adobe Community Professional ,
Jul 05, 2018 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
community guidelines
Community Beginner ,
Jul 05, 2018 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
community guidelines
Most Valuable Participant ,
Jul 05, 2018 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
community guidelines
Community Beginner ,
Jul 05, 2018 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
community guidelines
Most Valuable Participant ,
Jul 05, 2018 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
community guidelines
Community Beginner ,
Jul 05, 2018 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
community guidelines
Most Valuable Participant ,
Jul 05, 2018 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
community guidelines
Most Valuable Participant ,
Jul 05, 2018 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
community guidelines
Most Valuable Participant ,
Jul 05, 2018 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
community guidelines
Most Valuable Participant ,
Jul 05, 2018 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
community guidelines
Community Beginner ,
Jul 08, 2018 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
community guidelines
Adobe Community Professional ,
Jul 09, 2018 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
community guidelines
Adobe Community Professional ,
Jul 09, 2018 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
community guidelines
Community Beginner ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

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 .

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
community guidelines
Adobe Community Professional ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

Use something like this:

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

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
community guidelines
Most Valuable Participant ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

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.

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
community guidelines
Community Beginner ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

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;

}

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
community guidelines
Most Valuable Participant ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

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

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
community guidelines
Adobe Community Professional ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

This will not detect all combinations of София with upper and lower case and space.

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
community guidelines
Community Beginner ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

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


It's 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
community guidelines
Most Valuable Participant ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

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

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
community guidelines
Community Beginner ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

What I need?

I tested all combination with София  and it's 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
community guidelines
Community Beginner ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

You are right it does'nt work.

For example if I enter something different from "София" in the first time the value is 80, when I enter "София" the value is change to 100, now if I enter something different from "София" the value is not changed to 80 is 100....


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
community guidelines
Community Beginner ,
Jul 09, 2018 Jul 09, 2018

Copy link to clipboard

Copied

Yes, but I want to detect the all three combinations of София with upper and lower case and space.

How can I do it?

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
community guidelines