Confirming Matching Field Values in WPForms

Would you like to verify that users enter the same information in two different fields? While WPForms includes built-in email confirmation, you can extend this functionality to other field types like Phone, Address, or any other field where you need to confirm user input.

This guide will show you how to implement field comparison validation using a simple code snippet.

If you’re specifically interested in email confirmation, you can set this up directly in the form builder without any code by following our email confirmation guide.

Setting Up the Form

To begin, create a new form or edit an existing one. For this example, we’ll create a form with two Phone fields to demonstrate field comparison validation. The first field will collect the phone number, and the second field will be for confirmation.

If you need help creating a new form, please review our guide on creating your first form.

Adding the Code Snippet

Next, you’ll need to add the code snippet that handles the field comparison validation. If you’re not sure where to add custom code to your site, please check our guide on adding custom code.

The following snippet will compare two Phone fields and display an error message if they don’t match:

You’ll need to customize three key values in the code:

  1. On line 10, replace 1000 with your form ID
  2. On line 15, replace 3 with your first Phone field ID
  3. On line 18, replace 4 with your second Phone field ID

If you need help finding your form and field IDs, please check our guide on how to find form and field IDs.

When using Phone fields, you may need to adjust the pattern on line 21 ($pattern = '/^\d{10}$/';) to accommodate different phone number formats, especially for international numbers or custom formats. The current pattern expects exactly 10 digits.

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