I’m a massive fan of Gravity Forms, as it saves me considerable time with creating pretty complex contact forms. Most recently, I needed to create a contact form for a client where visitors are requested to enter their email address, confirm their email address and then enter their name. Once submitting the form, the users details are added to MailChimp using Gravity Form’s MailChimp Add On.
The motivation for adding an email address confirmation field was to help avoid visitors typing in the wrong email address, and therefore ensuring as many people as possible correctly sign up to the newsletter.

Since Gravity Forms does not currently natively support comparing values between fields, I needed to add my own custom hook to compare and validate the email address fields.
The Solution
First I check that I’m validating the correct form, as it’s pretty common for me to have more than one form per website.
Once the form has been validated, I check that the email address fields match (note that I’ve needed to hardcode absolute field names to refer to the two email address fields). The comparison is performed using a case insensitive search, just so that a minor difference in email address due to a capital letter is ignored.
If the fields are found to not match, the form is invalidated, and the 2nd email address field is marked as invalid with an appropriate error message. This approach works perfectly with AJAX-based forms too.
I hope that you find it useful!
/**
* Custom validation for gravity forms that will validate
* the 2nd email address field for a form that has an
* email address field, and a 'confirm your email address'
* field as the first 2 fields in a form.
*
* @author Dan Harrison (http://www.wpdoctors.co.uk)
*/
function custom_validation($validation_result)
{
// Extract the form from the validation result
$form = $validation_result["form"];
// Ensure we only validate the form entitled
// 'Newsletter Signup' otherwise return the
// result of the validation that's already been
// determined (i.e.
if ($form['title'] != 'Newsletter Signup') {
return $validation_result;
}
// We want to ensure that the 1st and 2nd fields
// match, as it's an email confirmation box
// First field (0 index) is the email address field
// Second field (1 index) is the field to confirm
// the email address
// Get the values from $_POST to do the
// comparison (watch the input numbers, as
// they're not 0-indexed as above).
// First field = $_POST['input_2']
// Second field = $_POST['input_3']
// Email addresses match (case insensitive, but
// use strcmp for case-sensitive comparison)
if (strcasecmp($_POST['input_2'], $_POST['input_3']) == 0)
{
// Nothing to do, fields already match
}
// Email addresses don't match.
else
{
// We're doing our own validation now, so
// override the validation for the whole form.
// Setting the default to false.
$validation_result["is_valid"] = false;
// Invalidate the 2nd email address
$form["fields"][1]["failed_validation"] = true;
$form["fields"][1]["validation_message"] =
"The email addresses do not match. ".
"Please check your email address is correct in both boxes.";
}
// Update the form in the validation result
// with the form object we've modified
$validation_result["form"] = $form;
// Return the validation result to
// continue the action.
return $validation_result;
}
add_filter('gform_validation', 'custom_validation');




This is awesome! Took me a little while to figure out how to change the code to get it to work but I got it.
Would love to know what you needed to change…
Thanks
Dan
where is this added in wordpress?
in the themefunctions file (functions.php) ?
Ok i figured out that the code above has to be added to the functions.php file.
keep in mind that if you use the gravity forms advanced fields that counting the fields may be different.
For example i used the advanced address field, which had street address, city, zip etc… but only counts as 1 field. So when entering the index be careful.
the best way to find out what the index is for the fields you are trying to match is to change the error index to see where the error appears.
by the way the next version of gravity forms (1.5) has email matching built in to the advanced email field.
the current beta release is too buggy for me to use tho (1.5 RC3).
Thanks Johnny, I’ll make a note to update the post when V1.5 of GF comes out. :)
Dan
Thanks for the function – read all the way to end only to discover that it may be included in 1.5 – just got that version now! hopefully it works! :)
Thanks Mistry. I’ve confirmed this and updated the article accordingly.
Dan
Hi Dan,
I prefer your solution (and I’m using v1.5.x.x …) over the new confirm e-mail function of Gravity Forms (it isn’t very flexible).
So, thanks a lot for providing your code, I only spend some time to figure out the [1] entries for “fields” :)
Regards,
Jack
Thanks Jack. Yeah, the array index needs changing based on your form! :)
Dan
Hi Dan
I came to your page after a lot of Google’ing.
I have a simple question . I have two radio buttons , Yes and No . What i want is when i choose No , an HTML element shows up and says some message. I want at the same time the submit button to get disabled . I simply don’t want to allow any one submit form if they choose “NO”.
Can This be done using a custom function like you did for email.
Plz do let me know
Kausar
Hi Kausar
How far have you got with this now?
Kind Regards
Dan
Thanks for posting. Even though I didn’t use this for email validation, I modified it to validate two fields that I needed to be the same for spelling reasons. The only thing you might add is that the validation is based on field position in the form. So I had to change the $form["fields"][1] to $form["fields"][6] and show validation on $form["fields"][7]. Maybe you could do it by id as well? that would be better in case the form ever got rearranged.
In any case, thanks so much!