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

Javascript Help for PDF Form creation

New Here ,
Sep 03, 2017 Sep 03, 2017

Hello,

I am looking for help writing the code for a PDF DC form. I want all the vowels in a string of entered data to appear red; while the other letters remain the same as preset. How do I do this? Thank in advance for your help!

c

TOPICS
PDF forms
1.3K
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
1 ACCEPTED SOLUTION
Community Expert ,
Sep 03, 2017 Sep 03, 2017

I like a good challenge, so I wrote it for you... Use this code as the field's custom validation script. Just make sure to tick the "Rich Text Formatting" box under the Options tab of the field's Properties.

The code:

var newValue = event.value;

var letters = newValue.split("");

var newRichValue = [];

for (var i in letters) {

    var span = {};

    span.text = letters;

    span.textColor = isVowel(letters) ? color.red : color.black;

    newRichValue.push(span);

}

event.richValue = newRichValue;

function isVowel(s) {

    return /^[aeiou]$/i.test(s);

}

View solution in original post

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 ,
Sep 03, 2017 Sep 03, 2017

That is quite tricky. You would need to use Rich Text Formatting and parse the value the user inputs, splitting it into single letter spans and applying the desired font style to each span, and then merge them into a single spans array and apply it back to the field's richValue property.

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 ,
Sep 03, 2017 Sep 03, 2017

I like a good challenge, so I wrote it for you... Use this code as the field's custom validation script. Just make sure to tick the "Rich Text Formatting" box under the Options tab of the field's Properties.

The code:

var newValue = event.value;

var letters = newValue.split("");

var newRichValue = [];

for (var i in letters) {

    var span = {};

    span.text = letters;

    span.textColor = isVowel(letters) ? color.red : color.black;

    newRichValue.push(span);

}

event.richValue = newRichValue;

function isVowel(s) {

    return /^[aeiou]$/i.test(s);

}

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
New Here ,
Sep 03, 2017 Sep 03, 2017

oh my gosh it works you are my hero!!!!!

One more questions... What if I wanted the consonants to be blue and not

black?

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 ,
Sep 03, 2017 Sep 03, 2017

In line #7 in the code above change color.black to color.blue...

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
New Here ,
Sep 03, 2017 Sep 03, 2017

Thank you !!!!

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 ,
Sep 06, 2017 Sep 06, 2017
LATEST

Thank you.

I have improved the script so that it can be used in French:

var newValue = event.value; 

var letters = newValue.split(""); 

var newRichValue = []; 

for (var i in letters) { 

    var span = {}; 

    span.text = letters

    span.textColor = isVowel(letters) ? color.red : color.blue; 

    newRichValue.push(span); 

event.richValue = newRichValue; 

 

function isVowel(s) { 

    return /^[aàäâeéèëêiïîoôuûùyÿ]$/i.test(s); 

}


Acrobate du PDF, InDesigner et Photoshopographe
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