As a marketer, one of your goals is to execute campaigns with a significant return on investment. That means that every lead you receive through your landing pages should be high value. Form field validation plays a pivotal role in guaranteeing the accuracy and usability of the collected lead data from your landing pages.
While Unbounce forms include some methods of basic form field validation, additional validation rules can be implemented using Regex to make sure your forms always capture correctly formatted data.
What is Regex?
Regex, short for Regular Expressions, is a powerful tool used to define search patterns in text. It allows you to create rules or patterns that text must adhere to, ensuring that only valid information is accepted. This precision enhances the accuracy and reliability of the data collected through lead generating landing pages.
In this article, you will learn how to implement custom form field validation into your landing pages using Regex.
Types of Form Field Validation Using Regex
Listed below are some of the more common use cases of Regex form field validation.
- Phone number validation: Verifies that the entered phone number matches a specific format, which may include country code, area code, and number grouping.
- Postal code validation: Validates the format of postal or ZIP codes based on the expected format for a particular country or region.
- URL validation: Verifies that the entered URL follows a valid format, including the protocol (e.g., "http://" or "https://"), domain name, and optional path or query parameters.
- Date format validation: Ensures that the entered date follows a specified format, such as YYYY-MM-DD or MM/DD/YYYY.
- Numeric input validation: Ensures that the entered value consists only of numeric characters and may include criteria such as minimum and maximum values.
- Alphanumeric input validation: Validates input containing both letters and numbers, allowing alphanumeric characters while restricting special characters.
- Business email validation: Requires the email address to belong to a business domain and not common personal email providers like Gmail, Hotmail, Yahoo, etc.
Adding Custom Regex Validation to Your Unbounce Form
For steps on how to add a form to your Classic Builder page, please review this article.
Once you have determined validation criteria for the form field(s) on your Unbounce landing page, you can add Custom Validation using Regex.
- Navigate to your page in the Classic Builder.
- Double click the form on your page. A new dialog box will appear.
- Select the form field where you wish to implement custom validation.
- Under Validation Options, enter your regex code into the Custom Validation field. The Common Regex Patterns section below offers patterns you can copy to use for custom validation.
- Enter an error message into the Validation Error message field. This is the error visitors will see when their form field input does not meet the validation criteria.
- Click Done to save the changes to your form.
- Remember to Save and Republish your page.
Note:
Pre-defined Email and Phone Number fields have their own built-in validation enabled by default. If you would like to add your own custom validation to these pre-defined fields, you first need to disable the built-in validation option:
Common Regex Patterns
The Regex patterns below can be copy and pasted into your landing page form to implement custom validation.
Validation Criteria | Regex Pattern | Description | |
Postal Code (US) |
^\d{5}(?:-\d{4})?$ |
Validates US postal codes in the format 12345 or 12345-6789. | |
Postal Code (Canada) |
^[ABCEGHJKLMNPRSTVXY]\d[A-Z]\d[A-Z]\d$ |
Validates Canadian postal codes in the format A1A 1A1. | |
URL |
^(http|https):\/\/[^\s$.?#].[^\s]*$ |
Validates URLs in the format http://example.com or https://example.com | |
Date (YYY-MM-DD) |
^(19|20)\d\d-(0[1-9] |
Validates dates in the format YYYY-MM-DD. |
|
Numeric Input |
^\d+$ |
Validates input consisting only of numeric characters. | |
Alphanumeric Input |
^[a-zA-Z0-9]+$ |
Validates input containing both letters and numbers, allowing alphanumeric characters only. | |
Business Email |
^(?!.*@(gmail\.com|yahoo\.com| hotmail\.com)).*$ |
Validate email addresses that have domains other than Gmail, Yahoo, or Hotmail |
FAQ
Can the Unbounce Support team write Regular Expressions for me?
No, writing Regular Expressions falls outside the scope of what our team can support. While we are happy to provide some examples in this documentation, we recommend working with a software developer or using third-party tools like ChatGPT to create your own custom Regular Expressions.