• First, absolutely love this plugin!

    I’ve been through the FAQ’s and Docs on the Contact Form 7 site and know how to limit the characters in a text or text* field. But the only thing i see for the textarea is setting the row and column height. For my field, I am collecting information about an author’s book but the description they enter needs to be 400 characters or less. How do I limit the textarea for character length?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Add this code into your active theme’s functions.php file:

    add_filter( 'wpcf7_validate_textarea', 'character_length_validation_filter', 11, 2 );
    add_filter( 'wpcf7_validate_textarea*', 'character_length_validation_filter', 11, 2 );
    
    function character_length_validation_filter( $result, $tag ) {
    	$name = $tag['name'];
    
    	if ( ! $result['valid'] )
    		return $result;
    
    	if ( 400 < strlen( $_POST[$name] ) ) {
    		$result['valid'] = false;
    		$result['reason'][$name] = "Too long.";
    	}
    
    	return $result;
    }

    This way, you’ll get a validation error when you enter more than 400 characters into textarea.

    Thread Starter psyon

    (@psyon)

    Thanks so much for the quick reply. I add the above code at the end of the functions.php file and got this error:

    Parse error: syntax error, unexpected T_FUNCTION, expecting T_STRING or T_VARIABLE or ‘$’ in /html/wp-content/themes/machine/functions.php on line 283

    Am I putting it in the right place? Should it go before or after a certain code segment?

    Also, is there a way to restrict the character limit to only one of the textarea fields? For example, if I have textarea A that should be 400 or less and textarea B that can be more than 400, is there a way to distinguish them?

    Thanks again for all your help!

    Pyson, could you share how you limit the characters in a text box? Looking to do the standard 3 box phone number setup with 3-3-4 characters set for each box so it automatically jumps to the next box after 3 characters are entered, etc.

    Thanks for any guidance.

    Thank you Takayuki Miyoshi !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Contact Form 7] Set character limit in textarea’ is closed to new replies.