Training, Help & How-to

Regular Expressions

The text fields allow a user to input any combination of text and/or numbers.  But sometimes you need to ensure what gets typed into a field matches a specific type of data like a phone number or an email address.  For example, let’s say we add a text element to a form called “Home Phone” that we want to always contain a valid U.S. phone number like “(800) 761-7760”.  Normally, the user could type anything into that field they want like “My shirt is blue”, which wouldn’t be what we want.

That’s where Regular Expressions come in.  Regular Expressions (or regex) provide us a way to describe what a valid response from the user looks like so that if they try to type something in a field that we didn’t want, we can tell them.  A regex describes a pattern of characters that matches the data we want.  An example of a regex that describes what valid U.S. phone number should look like is: \(\d{3}\)\s\d{3}-\d{4}.

If you’re not already familiar with regular expressions, this example might seem cryptic and confusing.  The good news is that for many standard cases, you can simply copy and paste ready-made examples we have below.  But, let’s break this example down:

NOTE:Here's the breakdown of the above regular expression:
"\("First thing we are expecting is a left parenthesis.
"\d{3}"This says that we are expecting exactly 3 decimal digits.  
The "\d" part says we are expecting digits and the 
"{3}" clarifies that we want exactly 3.
"\)"Next we are expecting a right parenthesis.
"\s"We want a space next.
"\d{3}"Just like abovewe want exactly 3 more digits.
"-"Next we want a hyphen character.
"\d{4}"Lastlywe want exactly 4 decimal digits

When the user types data into the text field and moves to the next field, FomMobi will test the data they entered against a regular expression to see if it matches.  If it doesn’t match, the text specified in the “Regular Expression Message” in the advanced property section is displayed to the user in red below the text field.

We have some pre-written regular expressions for common data types below that you can simply copy and paste.  Or, we’ve included some links below to other sites that have more pre-written regular expressions as well as tutorials on writing your own regular expressions.

Links to more information on regular expressions:

  • contains a huge searchable library of pre-written regular expressions for practically any purpose.  They also have an interactive online page where you can test any regular expression against text you supply. 
  • Regular Expressions Explained – A Tutorial
  • Another good tutorial
  • has printable reference cards that are great for those who understand regular expressions in theory but don’t have all the syntax memorized.

Pre-written Regular Expressions:

  • U.S. Phone Number - This is a slightly more complex expression than the one from above that allows for the area code to be surrounded by parenthesis, separated by a hyphen or a number of other combinations.
^(((\(\d{3}\)|\d{3})( |-|\.))|(\(\d{3}\)|\d{3}))?\d{3}( |-|\.)?\d{4}(( |-|\.)?([Ee]xt|[Xx])[.]?( |-|\.)?\d{4})?$ 
  • U.S. or International Phone Number
^([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)$ 
  • Simple Email Address
  • Website URL
  • U.S. Social Security Number
  • Currency
  • Percentage (0 to 100)
  • A Number Between -999.99 and +999.99
Search How-To
Most Viewed Documents
Related Documents
Related Videos