Skip to:
Content
Pages
Categories
Search
Top
Bottom

how to check if the length of field is too long?


  • jerryd221
    Participant

    @jerryd221

    Just like the check on the topic title, how to set a max length for group name, group description, message subject, message content, what’s new, username, password and/or even other extended profile fields?

    coz now long name completely breaks the page layout, especially in activity notice “%s created a group <very long group name>”.

Viewing 5 replies - 1 through 5 (of 5 total)

  • Henry Wright
    Moderator

    @henrywright

    You will need to make use of hooks and the strlen() function.

    For example, at registration, in order to validate the username so that it isn’t longer than 16 characters, you can do this:

    function my_username_validation() {
        // Perform validation here using the strlen() function to check the field length.
    }
    add_action( 'bp_signup_validate', 'my_username_validation' );

    danbp
    Participant

    @danbp

    Here an example for profile field lenght with some comments.

    function bpfr_custom_textfield_length() {	
    	
    	//Check if user is logged in & if xprofile component is activated
    	if ( is_user_logged_in() && bp_is_active( 'xprofile' ) ) :	
    	$my_custom_textfield = bp_get_member_profile_data( 'field=Brief Biography&user_id='.bp_get_member_user_id() );	
    	
    	/* 
    	 * The length = number of characters, not words.
    	 * Set the number of caracters to show. 
    	 * The 3 dots are the appended text ending the excerpt.
    	 * Don't remove the quotes if you change this
    	 * BuddyPress 2.1 will add new class and id for custom fields. 
    	 * The span can be omited to style this part. See ticket #5741
    	 */
    	if ( strlen($my_custom_textfield) > 20) :  //adjust to your need
    	$my_custom_textfield = substr($my_custom_textfield, 20).'...'; //adjust to your need
    	endif;
    	// uncomment the following line to get a span around the displayed field content
    	// echo '<span class="short-custom">'. $my_custom_textfield; .'</span>';
    	// comment the following line if you use the span
    	echo $my_custom_textfield;	
    	
    	endif; // is_user_logged_in
    }
    add_action( 'bp_directory_members_item', 'bpfr_custom_textfield_length' );

    Ticket #5741


    jerryd221
    Participant

    @jerryd221

    Thank you @Henry Wright and @danbp, these snippets are very useful. There is one more question about the validations, what if I want to stop the process right the way and show an error message accordingly to the user when the length of field exceeds the max length specified. Is it possible to do that inside the code snippets above?


    Henry Wright
    Moderator

    @henrywright

    Yes, I think you can. Try this:

    function my_username_validation() {
        if ( strlen( $_POST['signup_username'] ) > 16 ) {
            global $bp;
            $bp->signup->errors['signup_username'] = __( 'Put your error message here.', 'buddypress' );
        }
    }
    add_action( 'bp_signup_validate', 'my_username_validation' );

    jerryd221
    Participant

    @jerryd221

    cool, let me try this~~

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘how to check if the length of field is too long?’ is closed to new replies.
Skip to toolbar