Home / Admin / How to Block Names From Completing Your Form – First Last and First Middle Last format
Duplicate Snippet

Embed Snippet on Your Site

How to Block Names From Completing Your Form – First Last and First Middle Last format

<10
Code Preview
php
<?php
/**
 * 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 1000 and field ID is not 10
    if ( absint( $form_data[ 'id' ] ) !== 1000 || absint( $field_id ) !== 10 ) {
        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 );

Comments

Add a Comment