Over the past couple of months, our customers have asked us many questions about how to validate various types of user input; ranging from Zip Codes and Social Security Numbers to MAC Addresses and Hexadecimal codes. While the Cognito calculation syntax is pretty powerful and can already perform a couple of these functions, we wanted to provide a simple way for customers to implement their own text input validation; while also offering some advanced validation options for our power users to take advantage of.

User input from format validation dropdown.

In your Textbox field settings, you’ll notice the new Format Validation dropdown. From there, you can validate user input in a variety of different ways:

Standard Formats

Choose between several standard formats to validate your Textbox field:

  • Alphabetic
  • Numeric
  • Alphanumeric
  • SSN
  • Zip Code
  • IP Address
  • MAC Address

These formats are already set up and don’t require any calculations whatsoever; just select your format and you’re good to go. For example, the IP Address format requires users to enter a 4-number IP address, separated by periods (#.#.#.#.). If the address is not formatted correctly (maybe it contains too many or too few periods), an error message will appear:

A textbox field collecting IP Addresses in the correct format.

Meanwhile, the SSN format requires users to enter a 9-digit Social Security number, which is automatically reformatted to ###-##-####:

The SSN format collects 9-digit numbers with dashes.

Please note: You must encrypt any form and protect any field that contains sensitive information, such as: Social Security numbers, Driver’s license numbers, Banking information, and other personally identifiable information. Learn more about data encryption.

Custom Masks

If you need to collect data in a format other than the standard options, you can use a custom mask. A custom mask requires users to enter a numeric/alphabetic value in any format that you specify. When creating your mask, use # for numbers, @ for letters, and () around optional inputs. Then, write a custom error message:

Format mask used to validate specific string of characters.

When users don’t input their answers correctly, your error message will appear:

Error message when input does not match requirements.

Custom Regular Expressions

When you want to validate user input beyond just a sequence of numbers and letters, regular expressions (regex for short) allow you to validate or reformat very specific strings of text. For example; if you wanted a user to input a Hex color code (typically formatted as a “#” followed by 6 numbers or letters from A-F), you could use the following regular expression:

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
After you include an error message, the user will encounter an error if their code is not formatted correctly:

A textbox field displaying an error message when input is improperly formatted.

You also have the ability to reformat a regular expression by adjusting spaces, adding dashes, switching around groups, etc. For example; you can require users to enter either “abcd” or “ab cd” using the following expression:

^(ab) ?(cd)?
You can then reformat the expression to ensure that there is a space between “ab” and “cd”:

$1 $2
In this expression, “1” and “2” represent the two groups inside the two sets of parentheses, and we’ve added a single space between them. Now, if a users enters “abcd”, the Textbox will automatically reformat the string to include this space.

The Reformat expression automatically adds an extra space.

Regular expressions can be tricky depending on what you’re trying to achieve - you can check out this cheat sheet to review some of the basics. Additionally, when testing your expressions, RegExr is a great tool.

Have any questions about text input validation, or Cognito Forms in general? Contact us today!


Nadia Afshar

Nadia Afshar

Nadia, the Technical Writer for Cognito Forms, develops documentation, support content, and how-to videos. No matter your experience – with her help, you'll be able to take full advantage of the product. In her spare time, Nadia enjoys listening to French house, drinking coffee, and talking about herself in third person.