Regular Expressions

Updated on

In Sonic Forms you can use Regular Expressions (Regex) to validate user input in text fields. A Regular Expression is a specially coded statement which verifies if input meets certain criteria including:

- Minimum length
- Certain values or formats
- Minimum value (numeric or integer)
- Date formats and values

Regular Expressions are used in conjunction with specific error messages (e.g. "Must be more than $25.00) and allow you to prevent form entry if your criteria are not met.

The example below demonstrates only permitting numbers between 50 and 99. The regular expression in this case is: ^[5-9][0-9]$

And here is the form with a custom error message being displayed if the Regular Expression criteria is not met.

More Information
Using regular expressions you can validate numbers, names, email addresses, phone numbers, ZIP or postal codes, etc. Here is a list of commonly used regular expressions:
Pattern Description
^\d{5}$ 5 numeric digits, such as a US ZIP code.
^(\d{5})|(\d{5}-\d{4}$ 5 numeric digits, or 5 digits-dash-4 digits. This matches a US ZIP or US ZIP+4 format.
^(\d{5})(-\d{4})?$ Same as previous, but more efficient. Uses ? to make the -4 digits portion of the pattern optional, rather than requiring two separate patterns to be compared individually (via alternation).
^[+-]?\d+(\.\d+)?$ Matches any real number with optional sign.
^[+-]?\d*\.?\d*$ Same as above, but also matches empty string.
^(20|21|22|23|[01]\d)[0-5]\d$ Matches any 24-hour time value.
^[a-zA-Z0-9]*$ Alpha-numeric characters only
^[a-zA-Z]*$ Alphabetic characters only
^([ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9])*$ Canadian Postal Codes
^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$ Email Address
^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$ Phone Numbers (North American)

Here are some useful websites where you can find more regular expression examples: