How to set the maximum field length in InfoPath
Learn how you can use a data validation condition to check the maximum field length in InfoPath and display an error message to the user if the maximum field length is exceeded.
If you’re used to programming in Visual Studio, you’ll be disappointed to know that there is no MaxLength property that you can set on fields in InfoPath that can hold multiple lines of text with line breaks.
And if you are not looking to restrict the amount of characters that are typed into a multi-line text box, but rather restrict the amount of lines that can be typed into a text box, there is a bonus recipe in InfoPath 2013 Cookbook that shows you how to achieve this.
Note: You can limit the amount of characters on a single-line text box by going to the Properties dialog box of the text box, clicking on the Display tab, and selecting the Limit text box to checkbox.
When using data validation to restrict the maximum field length in InfoPath, you can use the string-length function in a The expression data validation condition to check whether the amount of characters entered in a field is greater than the maximum characters allowed, and then display an error message.
For example, in Figure 1, I’ve created a data validation condition to check whether the maximum length of a piece of text in a multi-line text box (field1) exceeds 10 characters. This technique also works for rich text boxes in InfoPath.
Caveat: Spaces and line breaks are counted as characters.
Figure 1. Data validation condition to check maximum field length in InfoPath.
Now whenever you type more than 10 characters into the field that has the data validation set on it, you’ll see a red border appear. And if you hover over the field, a tooltip containing the error message will appear as shown in Figure 2.
Figure 2. Error message in InfoPath when the maximum field length is exceeded.
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.