BuddyDev

Search

[Resolved] BP Username Changer – Field Validation

  • Participant
    Level: Initiated
    Posts: 5
    Renato Baccaro on #3388

    Hi,

    How can we add a custom BP error validation to the username?

    I need to make it alphanumeric and minimum of 5 chars.

    Please, advise.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #3389

    Hi Renato,

    Thank You for posting. You can create a custom error message by using the following code in your bp-custom.php file

    add_filter('bp_username_changer_validation_errors', 'buddydev_username_changer_custom_error_message', 3, 2 );
    function buddydev_username_changer_custom_error_message( $error, $new_user_name ) {
    
        if ( ! is_numeric( $new_user_name  ) ) {
            
            $error = new WP_Error( 'invalid', __( 'Username name must be numeric', 'bp-username-changer' ) );
        }
        
        if ( strlen ( $new_user_name ) < 5 ) {
            
            $error = new WP_Error( 'invalid', __( 'New user name can not be less than five character', 'bp-username-changer' ) );
        }
     
        return $error;
    }

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 5
    Renato Baccaro on #3390

    Hi Ravi, thank you for the prompt response.

    But !is_numeric will allow ALPHANUMERICS only?

    We want to avoid symbols etc.

    Thank you,

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #3391

    Hi Renato

    Please try this

    add_filter('bp_username_changer_validation_errors', 'buddydev_username_changer_custom_error_message', 3, 2 );
    function buddydev_username_changer_custom_error_message( $error, $new_user_name ) {
    
        if ( ! ctype_alnum ( $new_user_name  ) ) {
            
            $error = new WP_Error( 'invalid', __( 'Username name must be alphanumeric', 'bp-username-changer' ) );
        }
        
        if ( strlen ( $new_user_name ) < 5 ) {
            
            $error = new WP_Error( 'invalid', __( 'New user name can not be less than five character', 'bp-username-changer' ) );
        }
     
        return $error;
    }

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 5
    Renato Baccaro on #3393

    Great! Thanks!

  • Participant
    Level: Initiated
    Posts: 5
    Renato Baccaro on #3395

    Hi Ravi, the alphanumeric check worked, but the translation for the ERRORS did not.

    We just downloaded the 1.2.2 and add the custom original and translated error string in the PO file.

    Could you please advise?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #3397

    Hi Renato,

    Please put the translated text directly in the above code that I have posted. It won’t be loader from the pot file.

    Hope that helps.

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 5
    Renato Baccaro on #3398

    Yes, I managed to load the pot, it was a mistyping. Thank you!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2499
    Ravi on #3399

    Hi Renato,

    Thank You for confirming.

    Thank You
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved