How to Perform Field Comparisons Within Your WPForms

Interested in performing field comparisons within your form? While you may already know how to add a confirmation check for the Email field, what if you want to extend this capability to other fields like Phone or Address? Look no further! This tutorial will guide you through the straightforward process of achieving this using a concise PHP snippet.

If you’re interested in doing this for the Email field, please check out this documentation. You can create this comparison right inside the form builder!

Creating the form

To get started, let’s create a fresh form and include the necessary fields. In this tutorial, we’ll focus on crafting a form designed for visitors interested in obtaining a free quote. Consequently, we’ll add specific fields, including two distinct Phone fields: one for entering the phone number and another for confirming it.

create a new form and add your fields including 2 phone fields. one is for entering the number, the other phone field we will use for a confirmation

If you need any assistance in creating a new form, please review this helpful guide.

Performing the field comparisons

Now it’s time to add the snippet to your site. If you need help with where and how to add snippets, please take a look at this guide.

/**
 * Perform Field Comparisons
 *
 * @link https://wpforms.com/developers/how-to-perform-field-comparisons-within-your-wpforms/
 */

function wpf_dev_compare_fields( $fields, $entry, $form_data ) {
	
	// Only run this snippet on the form ID 3697
	if ( absint( $form_data[ 'id' ] ) !== 3697 ) {
    	return $fields;
	}

	// Enter the field ID for the first phone number
	$phone_1 = $fields[3][ 'value' ];
	
	// Enter the field ID for the second phone number
	$phone_2 = $fields[4][ 'value' ];

	// Regex pattern to match a simple 10-digit phone number
	$pattern = '/^\d{10}$/';

	// Now check if both phone numbers match the pattern
	if ( !preg_match( $pattern, $phone_1 ) || !preg_match( $pattern, $phone_2 ) || $phone_1 !== $phone_2 ) {
		wpforms()->process->errors[$form_data[ 'id' ]][ 'header' ] = esc_html__( 'Your phone number must be the same in both fields.', 'wpforms' );
	}

}
add_action( 'wpforms_process', 'wpf_dev_compare_fields', 10, 3 );

Please note that you’ll need to update the form and field IDs in the snippet to match your own IDs. For any help in finding your ID numbers, please check out this tutorial.

This snippet will look at the form ID 3697 and then grab the values entered on both Phone fields by looking that the field ID 5 and 7, if these fields don’t match, we’ll display an error message asking them to confirm the numbers entered.

using this snippet it is easy to perform field comparisons in your forms

If using any of the Phone fields for your validation, you may need to update this line of the code $pattern = '/^\d{10}$/'; to match the expected characters such as International and Smart fields that would include more than 10 characters for the country code or the international code format.

And that’s it! By using field comparisons, you can ensure the data being entered in your forms is verified. Would you like to conditionally display the Submit button? Take a look at our tutorial on How to Conditionally Show the Submit Button.

Reference Action

wpforms_process