Validate Form Fields with Regex
If leads in WhatConverts contain inaccurate or improperly formatted data, such as invalid email addresses or placeholder text in phone number fields, the issue is typically that your form fields are not validating input before submission. WhatConverts honors the HTML5 pattern attribute, which means you can use Regular Expressions (Regex) to prevent the form from submitting until a field matches a defined format. This article explains how to implement Regex validation to improve lead data quality.
Form fields are accepting inaccurate or incorrectly formatted input
Marking a field as required ensures it is not left blank, but it does not guarantee the data entered is valid. Without Regex validation, users can submit fields with placeholder text, incorrect formats, or spam content, all of which WhatConverts will capture as lead data.
How to add Regex validation to a form field
To troubleshoot this issue:
- Open your website's HTML and locate the <input> tag for the field you want to validate.
- Add the pattern attribute to the field along with the required attribute to ensure the field cannot be skipped.
- Use the appropriate Regex pattern for your validation goal. See the common patterns below.
- Test the form by entering incorrectly formatted data and confirming the browser prevents submission until the input matches the required pattern.
- Submit a correctly formatted test lead and verify the data appears accurately in your WhatConverts Lead Manager.
Example: Validating an email address field
<input type="email" name="email" pattern="^[a-zA-Z0-9.!#$%&'*+/=?^__{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)" required>
Common Regex patterns for lead field validation
Use the following patterns to address the most frequent lead data quality issues:
| Validation Goal | Regex Pattern Snippet |
| US Phone Number | pattern="^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$" |
| 5-Digit Zip Code | pattern="(\d{5}([\-]\d{4})?)" |
| Numbers Only | pattern="^[0-9]*$" |
| No URL/Links | pattern="^((?!http).)*$" (Useful for preventing spam) |
Note: Regex validation also helps prevent duplicate leads. When a field fails validation and the user corrects and resubmits, WhatConverts only captures the final successful submission. See the Fix Incomplete Form Lead Data article for more detail on how this works.
Related Topics
Get a FREE presentation of WhatConverts
One of our marketing experts will give you a full presentation of how WhatConverts can help you grow your business.
Schedule a Demo