17 March 2010

Convert Mixed Cased field to Lower, Upper and Title Case in InfoPath

Requirement: I have field on Infopath form that is free text box, say “FirstName”. We want to store the FirstName in title case (first character uppercase and rest lower).

Solution: I can to a regular expression check and ask users to enter data in correct format, but that will be quit limiting and reduces usability of the form. So, I decided to convert the case of the field after user enters the data. I accomplished this using no code. This solution is done using XPath expressions.

 

Convert FirstName to lower case:

translate(my:FirstName, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 



"abcdefghijklmnopqrstuvwxyz")




 



 



Convert FirstName to upper case:





 



translate(my:FirstName, "abcdefghijklmnopqrstuvwxyz", 



"ABCDEFGHIJKLMNOPQRSTUVWXYZ")




 



 



Convert FisrtName to Title Case:



This one is bit convoluted. We use concat, substring, translate and string-length infopath functions to accomplish this task. Idea is to divide the original string into two sub string:



first sub-string will the very first charecter that will converted to upper case.



second sub-string will be from second to last character, which will be converted to lower case.



Last step it to combine the two sub strings. Here how I do this:





 



concat(translate(substring(my:FirstName, 1, 1), "abcdefghijklmnopqrstuvwxyz", 



"ABCDEFGHIJKLMNOPQRSTUVWXYZ"), translate(substring(my:FirstName, 2, 



string-length(my:FirstName) - 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 



"abcdefghijklmnopqrstuvwxyz"))




 



Here how you can apply this formula in the rule after for the text area:



image



This is how it shows up:



 



image

0 comments: