Home > Back-end >  Wordpress - Add validation to First and Last name
Wordpress - Add validation to First and Last name

Time:12-06

I've been stuck on, in my opinion, a very simple and stupid problem. In the Wordpress User Backend, you have a First and Last name field. The only thing I want to accomplish is to add some form of validation to these fields. I also want to prevent Users from adding numbers to their first and last names in their WooCommerce account.

The problem

I cannot allow any numbers in the First and Last name fields, since WooCommerce uses this field for billing and shipping, and I use these fields for other purposes as well. I do not want to use plugins for this, and would like to accomplish this programmatically.

( BTW, it's also absolutely ridiculous even trying to Google this very problem. The only results are about custom fields and plugins, plugins, plugins... And the worst thing, is that none of those actually serve this purpose )

What I have tried

I have tried looking trough WP's admin user code, but I can't make heads or tails of it. I've seen ( a lot of ) suggestions about using plugins and using Custom Fields, but I don't want a plugin to do this for me, because of WooCommerce. I 'simply' want to add a validation to these 2 fields to prevent numbers in these fields.

Example of Checkout validation

Below is a code snippet that adds registration validation for a custom field. I was wondering if I could use this for the first and last name fields as well. I would like to use this for both the User registration form on the front-end, and the Wordpress back-end.

add_action( 'user_profile_update_errors','wooc_validate_custom_field', 10, 1 );

add_action( 'woocommerce_save_account_details_errors','wooc_validate_custom_field', 10, 1 );

function wooc_validate_custom_field( $args )
{
    if ( isset( $_POST['first_name'] ) )
    {
        if(preg_match('~[0-9] ~',$_POST['first_name'])) // if value contains a number
        $args->add( 'error', __( 'Numbers not allowed', 'woocommerce' ),'');
    }
}

Does anyone know how I could accomplish this? without using plugins please.. :)

CodePudding user response:

It sounds like you want to add a custom validation to the first and last name fields in WooCommerce. The code you posted looks like it's on the right track, but it only checks the first_name field. To check the last name field as well, you could add another check like this:

if ( isset( $_POST['first_name'] ) && isset( $_POST['last_name'] ) )
{
    if(preg_match('~[0-9] ~',$_POST['first_name']) || preg_match('~[0-9] ~',$_POST['last_name'])) // if value contains a number
    $args->add( 'error', __( 'Numbers not allowed in first or last name', 'woocommerce' ),'');
}

This code will check both the first and last name fields and add an error message if either of them contain numbers. Note that this code will only work on the front-end user registration form and the "Edit Account" form in WooCommerce. It won't affect the first and last name fields in the WordPress user backend.

If you want to add the same validation to the WordPress user backend, you can use the edit_user_profile_update hook. This hook is triggered when a user updates their profile in the WordPress backend. You can use it like this:

add_action( 'edit_user_profile_update', 'my_validate_first_last_names', 10, 1 );

function my_validate_first_last_names( $user_id )
{
    $user = get_userdata( $user_id );
    $first_name = $user->first_name;
    $last_name = $user->last_name;

    if( preg_match('~[0-9] ~', $first_name) || preg_match('~[0-9] ~', $last_name) )
    {
        // Add an error message
        $error_message = __( 'Numbers are not allowed in the first or last name fields', 'woocommerce' );
        $user->add_error( 'first_last_name_error', $error_message );
    }
}

This code will retrieve the user's first and last names from the database, check if they contain numbers, and add an error message if they do.

I hope this helps! Let me know if you have any other questions.

  • Related