Restrict numbers from being used in the first and last name fields on checkout in WooCommerce

Restrict numbers from being used in the first and last name fields on checkout in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action('woocommerce_after_checkout_validation', function ( $fields, $errors ) {

    if (!preg_match('/^[a-zA-Z]+$/', $fields['billing_first_name'])) {
        $errors->add('billing_first_name', 'First name must contain only alphabetic characters');
    }

    if (!preg_match('/^[a-zA-Z]+$/', $fields['billing_last_name'])) {
        $errors->add('billing_last_name', 'Last name must contain only alphabetic characters');
    }
}, 10, 2);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.