Thursday, November 11, 2010

Limit the number of characters in InfoPath multi-line boxes

Situation: we have a textbox we made in InfoPath. We decide that this box should be able to type multiple lines. We open the settings for the box and change click the checkbox that says "Multiline". It then greys out our ability to limit the number of characters in the box. We want to still limit how many letters that they can type! Here's how:

Use Data Validation on the box (in 2007, right-click the textbox and then click "Data Validation", click "Add"; in 2010, click the textbox, click the Control Properties in the ribbon, and click "Manage Rules", then click Add -> Validation rule, then click the condition).
[Update: a comment from a user below highlighted a limit in the solution: line breaks. A better way to accomplish this is to use the 'the expression' in the first dropdown instead of your field and then type "string-length(.)>500" without the quotes]
Now that you have the data validation window open you should see the ability to set your condition. The first box should be your field, the middle box should say "Does not match pattern", and then you click "Select a pattern" in the third box -> click custom pattern, and then type this:

.{1,500}

See, the Period stands for any character except new line breaks or carriage returns. Now, normally you would put an asterisk after that to mean that you can type as much as you want. Instead, the little braces allow you to place lower and upper limits for the number of characters. The 1 means that they HAVE to type at least 1 letter and the 500 means they get capped off at 500 for data validation purposes.

Now, this does NOT mean that the InfoPath box will STOP letting them type! It just means that, when they tab out of it, it will glow red and have a tooltip that you specify for them when they've typed too many characters. I tested this and it seems to work well for me; give it a try and tell me your results with it!