Contact forms help website visitors ask questions, request services, submit inquiries, and share information with a business. However, accepting every input without checking whether it meets your requirements can create problems.
Visitors may enter incomplete details, use an incorrect phone number format, provide mismatched email addresses, or submit information that your business cannot use.
Contact Form 7 also lets you manage submitted entries, making it easier to review the information visitors provide through your forms. See our guide to Contact Form 7 Submissions for more details.
For example, you might want visitors to enter a specific phone number format, confirm their email address, or provide a minimum amount of information before submitting a form.
This is where Contact Form 7 Custom Validation becomes useful. By adding custom validation rules, you can check submitted information against your requirements and display helpful error messages when an entry does not meet those conditions.
In this guide, you will learn how to add custom validation to Contact Form 7 using its built-in settings and PHP validation hooks, with practical examples and troubleshooting tips.
What Is Custom Validation in Contact Form 7?
Custom validation in Contact Form 7 means adding your own rules to check the information visitors enter before a form submission is accepted.
Standard validation handles common requirements, such as checking whether a required field is empty or whether an email address follows an acceptable format. However, it cannot automatically understand every business-specific requirement.
For example, consider a service inquiry form. You may want to:
- Require visitors to enter a business email address.
- Check whether two email fields contain matching addresses.
- Restrict a phone number to an accepted format.
- Set minimum or maximum character requirements.
- Prevent visitors from submitting incomplete project details.
- Display a specific error message when an input does not meet your requirements.
These additional conditions can be implemented through custom validation.
The main purpose is not to make forms more complicated. It is to collect accurate, relevant information while helping visitors understand what they need to correct.
Why Do You Need Custom Validation Rules?
Not every website collects the same information. A basic contact form may only need a name, email address, and message. However, a business receiving product inquiries, quotation requests, or service applications may need more detailed information.
Here are some common situations where Custom Validation in Contact Form 7 can help.
1. Collect accurate contact information
Incorrect email addresses and incomplete phone numbers can make it difficult to respond to customer inquiries. Additional validation rules help check whether visitors provide information in the expected format.
2. Reduce incomplete form submissions
Some inquiries require specific details before a business can provide an answer. You can add validation rules to ensure that important fields contain the information your team needs.
3. Improve the visitor experience
Clear validation messages help visitors understand why a form cannot be submitted. Instead of displaying a general error, you can explain exactly what needs to be corrected.
4. Apply business-specific requirements
A quotation form, job application, and product inquiry form have different requirements. Custom rules allow you to adjust form validation according to the information each process needs.
5. Maintain consistent submission data
When visitors follow the same input requirements, your team can review and process submitted information more consistently.
Built-in Validation vs. Custom Validation in Contact Form 7
Before writing custom code, understand what Contact Form 7 already supports. You may not need additional PHP for common validation requirements.
| Validation type | What it checks |
| Required field | Whether visitors complete a required field |
| Email validation | Whether an email address follows an acceptable format |
| Telephone field | Whether the input meets the field’s telephone validation |
| Number validation | Whether a number meets configured limits |
| URL validation | Whether the entered URL is valid |
| File validation | Whether an uploaded file meets supported requirements |
| Custom validation | Whether information follows your own business rules |
For example, adding an asterisk to a field makes it required:
[text* your-name]
Similarly, an email field can be configured as:
[email* your-email]
These options handle common requirements without additional code.
However, if you want to compare two email fields or apply a rule that Contact Form 7 does not provide by default, you may need custom PHP validation.
How to Add Custom Validation to Contact Form 7
There are several ways to improve form validation. The right approach depends on whether you need a standard field requirement or a completely new validation condition.
Step 1: Open Your Contact Form 7 Form
First, make sure Contact Form 7 is installed and activated on your WordPress website.
- Log in to your WordPress dashboard.
- Navigate to Contact → Contact Forms.
- Select the form you want to edit.
- Open the Form tab.
- Review the existing fields and identify which inputs need additional validation.
If you have not created a form yet, start by following this tutorial on adding a contact form.
For example, a basic form might contain:
[text* your-name placeholder “Your Name”]
[email* your-email placeholder “Your Email”]
[tel your-phone placeholder “Phone Number”]
[textarea your-message placeholder “Your Message”]
[submit “Send Message”]
The asterisk indicates that a field is required. You can adjust these fields according to your website’s needs.
Step 2: Understand the Validation Requirements
Before adding code, decide exactly what information you want to validate.
For example, suppose your form collects an email address and asks visitors to enter it again for confirmation.
Your validation requirement is simple:
- Both fields must contain an email address.
- The second email address must match the first one.
- If the addresses do not match, the form should display an error message.
Contact Form 7 does not provide a built-in email confirmation field. You can implement this requirement using a custom validation filter.
Step 3: Add a Confirmation Email Field
Return to the Form tab and add another email field below your existing email input.
Use the following form tags:
[email* your-email placeholder “Email Address”]
[email* your-email-confirm placeholder “Confirm Email Address”]
Both fields are required. The second field will be checked against the first one using custom PHP validation.
Make sure the field names in your form match the names used in your PHP code. Otherwise, the validation function will not identify the correct inputs.
Step 4: Add Custom Validation Using PHP
Contact Form 7 provides validation filters that allow developers to add their own input checks.
For email fields, the relevant validation hook is wpcf7_validate_email*. The asterisk indicates a required email field.
Add the following code to your child theme’s functions.php file or use a PHP code snippet manager.
add_filter(
‘wpcf7_validate_email*’,
‘pluginorbit_email_confirmation_validation’,
20,
2
);
function pluginorbit_email_confirmation_validation( $result, $tag ) {
if ( ‘your-email-confirm’ === $tag->name ) {
$your_email = isset( $_POST[‘your-email’] )
? sanitize_email( wp_unslash( $_POST[‘your-email’] ) )
: ”;
$confirm_email = isset( $_POST[‘your-email-confirm’] )
? sanitize_email( wp_unslash( $_POST[‘your-email-confirm’] ) )
: ”;
if ( strcasecmp( $your_email, $confirm_email ) !== 0 ) {
$result->invalidate(
$tag,
‘The email addresses do not match. Please check both fields.’
);
}
}
return $result;
}
How does this code work?
- add_filter() connects your function to Contact Form 7’s email validation process.
- wpcf7_validate_email* targets required email fields.
- $tag->name identifies the field being checked.
- sanitize_email() cleans the submitted email values.
- strcasecmp() compares the two addresses without treating letter case as a difference.
- $result->invalidate() marks the field as invalid and displays your custom message.
- return $result passes the validation result back to Contact Form 7.
The code applies the additional check only to the confirmation field named your-email-confirm.
Important: This is a PHP example for developers. Test it on a staging website before using it on a live form. Back up your site and make sure your form field names match the code.
Step 5: Save and Test Your Custom Validation
After adding the code, save your changes and open the page containing your contact form.
Test the following situations:
- Enter the same email address in both fields.
- Enter different email addresses.
- Leave one of the required fields empty.
- Enter an incorrectly formatted email address.
- Submit the form with valid information.
When the two email addresses do not match, the form should display your custom validation message. When the values match and all other requirements are satisfied, the form should pass this additional validation check.
Also check whether the form displays the expected messages on desktop and mobile devices.
Other Contact Form 7 Validation Rules You Can Implement
Email confirmation is only one example. Depending on your website’s requirements, you can implement other Contact Form 7 Validation Rules using the appropriate validation filters.
1. Validate phone number length
A telephone field may need additional checks if your business accepts a particular range of digits.
For example, you can create a rule that accepts between 10 and 15 digits after removing spaces, brackets, and common phone number separators.
Use the wpcf7_validate_tel* filter for a required telephone field. Keep international phone number formats in mind rather than assuming every visitor uses the same country’s numbering system.
2. Restrict specific email domains
If your business only accepts registrations from approved organizations, you can validate the email domain.
For example, you might allow addresses from a particular company domain while rejecting personal email providers.
However, apply this rule only when your business genuinely needs it. Restricting email providers unnecessarily can prevent legitimate customers from contacting you.
3. Set minimum and maximum character limits
Some forms require more detailed information than others. A project inquiry form, for example, may need a minimum description length.
You can use custom validation to check the submitted text length and display an appropriate message when it falls outside your requirements.
4. Validate product selection fields
A product inquiry form may need visitors to select a product before submitting their request.
If you use additional product fields, check that the submitted selection is valid and corresponds to an available option. This is particularly useful when building product inquiry forms with Contact Form 7.
For additional product selection functionality, see the Product Dropdown Selector for Contact Form 7.
How to Customize Contact Form 7 Validation Error Messages
Validation is more useful when visitors understand why their information was rejected.
Contact Form 7 allows you to edit several default messages through its Messages tab.
To customize them:
- Open Contact → Contact Forms.
- Select your form.
- Open the Messages tab.
- Find the relevant validation message.
- Replace the default text with a clear explanation.
- Save your changes.
For example, instead of a vague message, you could use:
- Please enter your full name.
- Enter a valid business email address.
- The email addresses do not match.
- Please provide a valid telephone number.
- Select a product before submitting your inquiry.
For custom PHP validation, you can also provide a specific message through the invalidate() method, as shown in the earlier example.
Keep messages short, polite, and actionable. Visitors should know what to correct without having to guess.
You can also Styling Contact Form 7 to make validation errors easier to notice and understand.
Common Contact Form 7 Validation Problems and How to Fix Them
Even correctly configured validation can behave unexpectedly when field names, hooks, or other website components are not configured properly.
Here are some issues worth checking.
Custom validation is not working
Check whether the validation hook matches the field type. For example, a required email field uses wpcf7_validate_email*, while a required telephone field uses wpcf7_validate_tel*.
Also confirm that the field name in your PHP condition matches the form tag.
Validation error messages are not displayed
Make sure your code calls $result->invalidate() when a condition fails and returns $result after validation.
Also check the Messages tab and any custom styling that may hide error messages.
Form submissions fail after adding PHP code
A syntax error or incorrectly placed function can affect your WordPress website. Review the PHP code, check your error logs, and temporarily disable the snippet if necessary.
Test changes in a staging environment before deploying them to your live website.
Validation works on one form but affects another
If the same validation function runs on multiple forms, check whether it targets the correct field name. For more complex websites, you may also need to restrict the logic to a particular form.
Visitors submit the form, but no email arrives
Validation and email delivery are separate processes. A form can pass validation successfully while its notification email still fails.
If this happens, review your mail settings andContact Form 7 Emails Not Sending.
Best Practices for WordPress Form Validation
When implementing WordPress Form Validation, the objective should be to improve data quality without making forms unnecessarily difficult to complete.
Keep these practices in mind:
- Validate only necessary information: Avoid adding restrictions that do not support a genuine business requirement.
- Use clear error messages: Explain what visitors need to correct.
- Check server-side input: Browser-side validation can improve convenience, but PHP validation is important because client-side checks can be bypassed.
- Test different input formats: Consider international phone numbers, email addresses, and different visitor devices.
- Avoid unnecessary field duplication: Ask visitors to confirm information only when there is a practical reason.
- Keep your website updated: Maintain WordPress, Contact Form 7, themes, and related plugins.
- Review submitted entries: Check whether your validation rules are collecting the information your team actually needs.
You should also consider how submissions are stored and reviewed. If your website needs a record of visitor inquiries, you can save Contact Form 7 submissions.
Conclusion
Adding custom validation to Contact Form 7 helps WordPress website owners collect more accurate information and apply rules that match their business requirements. While built-in validation handles common input checks, PHP validation filters allow developers to implement additional conditions such as email confirmation, phone number restrictions, and custom field requirements.
Start by identifying the information your form needs, use built-in validation wherever possible, and add custom PHP rules only when necessary. Test each condition before publishing the form, and keep error messages clear so visitors can correct their information easily.
With the right Custom Form Validation, you can make your Contact Form 7 forms more reliable, improve the quality of incoming inquiries, and create a smoother submission experience.


