InfoPath data validation using regular expressions

InfoPath data validation using regular expressions

I found it interesting yesterday, while building an InfoPath template for SharePoint 2007, that you can use regular expression code to perform data validation the same way it’s used in an ASP.Net form, and the same way it’s done in C#. Here’s an example:

In my form, I wanted to make sure the data entered conformed to a specific email address format. Normally, an email address pattern looks like this:

w+([-+.’]w+)*@w+([-.]w+)*.w+([-.]w+)*

This won’t work in InfoPath, there are too many unrecognizable characters. However, very similarly you can force a pattern match in InfoPath by doing something like this:

p{L}+@p{L}+.p{L}+

Unlike C# and ASP.Net, InfoPath data validation has some explanation of the code behind their form of regular expression validation. C# regular expression code is all over the internet, a Google search will provide a wealth of help on it, and I’ve blogged about it before. But I found it interesting that you can use regular expression for data input validation, not to mention to promote security, preventing SQL injection and launching of unsafe code. If you’re well versed in InfoPath you already knew this. If not, give it a try by right-clicking on the TextBox control, for example, and choose Data Validation. Under the “If condition is true” section, the third drop down will say “Select a Pattern”. Choose this, and in the Data Entry Pattern window, under Standard Patterns choose Custom Pattern. Then use the Insert Special Character drop down below to help you build your expression. Like regular expressions, you can force specific words, too. So something like:

p{L}+@gmail.com

Would be the same as saying “allow any word before @gmail.com”. The p{L} allows any letter, the “+” immediately after that says “multiple”. As you can see, there’s a lot of possibilities with the way to construct expressions. Give it a try the next time you’re in InfoPath!

Comments are closed.