Catching apostrophes in text data elements with program rules

I would like to validate that there are no apostrophes (') in a data element (valuetype text) with d2:validatePattern (docs).

What I tried:

blocklist (not allowing the apostroph in the value):

d2:validatePattern( #{text_de}, '.*\'.*' )
d2:validatePattern( #{text_de}, '.*\\'.*' )
d2:validatePattern( #{text_de}, '.*\\\'*.' )
d2:validatePattern( #{text_de}, '.*\\\\'*.' )
d2:validatePattern( #{text_de}, '.*\\\\\'*.' )

switching out the enclosing apostrophes to "

d2:validatePattern( #{text_de}, ".*\'.*" )

allowlist (checking if the value matches a pre-defined character set):

!d2:validatePattern( #{text_de}, '^[a-zA-Z0-9\\s]+$' )

alternative functions:
splitting value by the apostroph and checking if the first part is not empty:

d2:hasValue( #{text_de} ) && d2:split( #{text_de}, '\'', 0) != ''

They all do not trigger when a user enters an apostroph. Is there a way to achieve this?

1 Like

Hey @dhuser,

Does using it without the slash work?

Thanks! (:

Thanks for checking. Unfortunately it doesn’t – I did a test, the validatePattern unit test also fails (to trigger in Capture) if it is using double apostrophes, e.g. comparing:

A) tested, works as per unit test and Capture app:

d2:validatePattern(#{test_var_one}, '.*555.*')

B) does not work in Capture app:

d2:validatePattern(#{test_var_one}, ".*555.*")

So I believe there currently is no support for escaping the apostroph, nor alternative characters to wrap the expression.

1 Like