Character only validation for first name and last name in WooCommerce

Character only validation for first name and last name in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'woocommerce_after_checkout_validation', 'bks_validate_fname_lname', 10, 2);

function bks_validate_fname_lname( $fields, $errors ){
    if (!preg_match( '/^[a-zA-Z ]+$/', $fields[ 'billing_first_name' ])){
        $errors->add( 'validation', 'First name should only have no special characters.' );
    }
    if (!preg_match( '/^[a-zA-Z ]+$/', $fields[ 'billing_last_name' ])){
        $errors->add( 'validation', 'Last name should only have no special characters.' );
    }
}