Validating User Input with Regular Expressions and the TekDog Input Mask

When we build solutions at TekDog, we often have a situation where the user registration page requires an email address. Guess how often the email address is invalid as typed by the user? Incorrect email addresses can really be a nuisance when trying to send out a mailing list for instance. Also, if the email is intended to be the account username, all sorts of havoc can ensue. Good luck trying to reset your password! This is why many website registration pages ask you to type your email address a second time to confirm.

What if you could validate the correct syntax of email address right in a single line text box?

The problem of validating an email address is the basis for TekDog’s Input Mask control. The Input Mask control allows you to select from a set of pre-defined formats like email address, phone number, website URL, postal address, etc. But possibly the most powerful and customizable selection is the Regular Expression or RegEx validation. And you can use Regular Expressions in Nintex Forms even without the Input Mask. Here’s how.

A Regular Expression is a fancy term for what some of you might call a wildcard. You might know that in a Windows command line you can type dir *.txt and all files that match anything ending with .txt will be matched and listed. Regular Expressions are like wildcards but wayyy more powerful. And Regular Expression is a mouthful itself, so it’s often just called RegEx.

When used to validate user input, RegEx is used to test whether the input matches a certain pattern. If it doesn’t match then the input is not valid. Let’s say we want to validate whether the input is a reasonable, valid phone number with dashes. We want to create a RegEx pattern that will allow only numbers and dashes, and have the dashes in the right spot.

The token for matching a numeric digit in RegEx is \d. That’s a backslash with a “d” after it. In RegEx, you can match several of the same type of character in a row by putting curly braces with a number after the token. So \d{3} will match three digits in a row. It’s easy to use Google and find plenty of documentation on other RegEx tokens so I won’t go into that here. You can also find RegEx testing tools online.

So to match a complete phone number with an area code, our RegEx should be:
^\d{3}-\d{3}-\d{4}$

That will match three digits and a dash then three more digits and a dash and four more digits. The caret (^) and dollar sign ($) match the beginning and ending of the input respectively. Without them, the user could type characters before or after the phone number and still match.

In Nintex Forms, you can specify RegEx as a custom validator on a Single Line Text Box settings in the Validation section by selecting Yes for “Use a regular expression” and then typing the RegEx in the given text box.

blog-regularexpress1.png
blog-regularexpress2.png

Our Productivity Controls for Nintex Forms package includes an Input Mask Control that also can validate with RegEx. In addition to a custom regular expression like the phone number above, you can choose from a list of other pre-defined regular expressions.

blog-regularexpress3.png

The regular expression used for the Input Mask control is quite a bit more sophisticated in that it can accommodate and correct little errors like extra spaces, and can allow a phone number extension typed after the phone number.

blog-regularexpress4.png
blog-regularexpress5.png

Likewise, the other selections for pre-defined Input Masks use complex RegEx that you’ll never have to see.

Happy validating!