Home > Back-end >  Javascript: How to compare against substring in PDF forms?
Javascript: How to compare against substring in PDF forms?

Time:09-30

I am trying to use some Javascript in a PDF form. As an example, let's say I have two fields: let's call them TextField1 and TextField2.

These fields can be used independently, but when TextField1 has a certain value, I'd like TextField2 prefilled.

This works fine (triggered onBlur, not that it matters):

var f = this.getField("TextField1");
var g = this.getField("TextField2");

if (f.value == "123") {g.value = "foobar";}

My problem is this: How can I compare against a substring of f? I'd like the action to trigger when the first char of f equals "1", but

if (f.value.substr(0,1) == "1") { … } 

or similar have not worked.

CodePudding user response:

Try to make a string instance from value.

You can use one of the following methods:

var fStr = f.value   '';
var fStr = String(f.value);

and then try to test fStr in a manner you mentioned above: fStr.substr(0,1) === 'a'

  • Related