Jeremy Stein - Brain

« »

InfoPath conditional text

I’m using InfoPath 2010. If I want some text to display a different value depending on the value of a field and I don’t want to use section hiding to accomplish it, I can use a Calculated Value control like this:

When field1 is “value1”, I want to display “Some Text”. When field1 is “value2”, I want to display “Other Text”. Here’s the xpath expression:

concat(substring("Some Text", (my:field1 != "value1") * 999), substring("Other Text", (my:field1 != "value2") * 999))

How does it work? Normally, the expression (my:field1 != "value1") evaluates to 1 since it’s true. If you multiply that by 999, it’s way past the end of the string, and starting the substring there gives you nothing. But, when it evaluates to 0, the substring function returns the whole string and when concatenated with the other blank strings, gives you just the text you want.

May 9, 2012 2 Comments.

2 Comments

  1. Usman replied:

    Excellent post, Jeremy. I had this issue, and it worked like a charm
    thanks for posting it.
    Btw, i believe if you had used 1 instead of 999 it will work 🙂

    January 11th, 2013 at 2:23 pm. Permalink.

  2. Jeremy replied:

    1 would only work if your output strings were only one character long. Try it.

    January 11th, 2013 at 2:27 pm. Permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »