How to Block Names From Completing Your Form

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.

Blocking names from your form

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.

Simple format

/**
 * Prevent certain names from the Simple format 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 || absint( $field_id ) !== 1 ) {
        return;
    }

    // Create your list of blocked names separated by commas
    $blocked_names = array( 
        'Jane Doe', 
		'John Doe',
        'Jack Doe'
    );
 
    foreach( $blocked_names as $name ) {
        if(strpos($field_submit, $name) !== FALSE ) {
            wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = 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 );

First Last and First Middle Last format

/**
 * Prevent certain names from the other formats for 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 || absint( $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( preg_match( '/\b' .$name. '\b/i',$submitted_name) ) {
            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.