How to Block Names From Completing Your Form

Introduction

Would you like to block names from being able to submit your forms? You can easily block specific names from being able to complete your form using a little extra validation. In this tutorial, we’re going to walk you through each step on how to use this snippet to prevent these names from completing your form.

Creating the form

First, you’ll need to create your form and add your fields. If you need assistance in how to create a form, please review this documentation.

create your form and add all of your form fields including the name field

Setting the format for the Name field

Next, you’ll need to define the Format for the Name field. In this tutorial, we’re going to use First Last as the format.

Adding the snippet to block names

Now it’s time to add the snippet that will block these names from completing your form.

If you need help in adding snippets to your site, please check out this tutorial.

/**
 * Prevent certain names from the Name form field.
 *
 * @link https://wpforms.com/developers/how-to-block-names-from-completing-your-form/
 *
 */
 
function wpf_dev_block_name_validation( $field_id, $field_submit, $form_data ) {
 
    // Bail early if form ID is not 879 and field ID is not 1
    if ( absint( $form_data['id'] ) !== 879 && $field_id !== 1 ) {
        return;
    }
	
	// Form builder Name format is set to First Last
	$submitted_name = $field_submit['first'] .' ' .$field_submit[ 'last' ];
	
    // Create your list of names to be blocked separated by commas
    $blocked_names = array( 
        'John Doe', 
        'Jane Doe',
	    'Jack Doe'
    );
  
    foreach( $blocked_names as $name ) {
        if( strcmp( ucwords($submitted_name), $name ) == 0 ) {
            wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Can\'t submit form. Please try again later.', 'wpforms' );
            return;
        }
    }
 
}
 
add_action( 'wpforms_process_validate_name', 'wpf_dev_block_name_validation', 10, 3 );

This snippet will only run on the form ID 879 and only process on the form field ID of 1. You’ll need to update these two ID numbers to match the form ID of your form and the field ID number that your name field.

If you need any help in locating your form and field IDs, please take a look at this tutorial.

The next section of the snippet will assign the First and Last name to a variable that we’ll use when we’re checking the name entered against the blocked name list we’re creating with the $blocked_names.

In the foreach loop, we need to make sure that even if the user doesn’t type in a name with capital letters, we’re converting it inside the snippet.

So now if any names on the $blocked_list try to submit the form, they will see our error message.

the snippet will block names from your list submitting the form

And that’s all you need, would you like to also block profanity on your form? Take a look at our article on How to Block Form Submissions Containing Profanity.

FAQ

Q: What if I’m using a different Name format?

A: If you aren’t using the First Last name format for your Name field, you can update the snippet.

Simply replace this line

$submitted_name = $field_submit['first'] .' ' .$field_submit[ 'last' ];

with this line if you are using the First Middle Last.

$submitted_name = $field_submit['first'] .' ' .$field_submit[ 'middle' ] .' ' .$field_submit[ 'last' ];

If you are using just the Simple format for the name, replace the line with this.

$submitted_name = $field_submit;

When trying to block names, using the First Middle Last is not advisable as there are too many different ways that the submission could get through on your form (initials only, full middle name etc).