How to Deny Specific Phone Numbers From Submitting

Introduction

Would you like to deny specific phone numbers from submitting entries on your forms? You can easily create a small PHP snippet that will block the form from submitting if the phone number is found on the denial list. In this tutorial, we’ll walk you through the steps on how to achieve this.

Creating your form

First, you’ll need to begin by creating a new form and adding your fields to this form which will include a Phone form field.

In our tutorial, we’re going to use the Smart format for our Phone field.

If you need assistance in creating your form, please see this documentation.

create your form add your phone field and the rest of your fields

Denying specific phone numbers

In order to deny specific phone numbers, you’ll need to add this snippet to your site.

If you need help in how to add snippets to your site, please see this tutorial.

/*
 * Deny specific phone numbers from your WPForms.
 *
 * @link https://wpforms.com/developers/wpforms_process_validate_email/
*/
 
function wpf_dev_blacklist_phone( $field_id, $field_submit, $form_data ) {

    // List each number in this array and separate with a comma using one of the formats detailed below

    // Smart         Phone Format - +12025550164
    // US            Phone Format - (202) 555-0164
    // International Phone Format - 2025550164

    $phone_blacklist = array( 
            '+12025550164',
            
    );
 
    if( in_array( $field_submit, $phone_blacklist ) ) { 
        wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Phone not accepted!', 'wpforms' );
        return;
    }
}

add_action('wpforms_process_validate_phone', 'wpf_dev_blacklist_phone', 10, 3 );

The $phone_blacklist is a list of numbers that you will create which will stop the form from submitting if one of the phone numbers entered on the form matches a number in this list.

The above snippet is based off using the Smart Format for the Phone field. Please see the comments in the snippet above to learn more about the US or International Format.

with this snippet you can now deny specific phone numbers from submitting your form

And that’s all you need! Would you like to also turn the phone number into a link inside the email notification? Take a look at our tutorial on How to Make Phone Numbers a Link in Email Notifications.

Action Reference: wpforms_process_validate_phone