Support

Account

Home Forums General Issues Minimum number of characters in a text field Reply To: Minimum number of characters in a text field

  • As the ACF GUI only gives the option for a maximum character limit, you can use the acf/validate_value filter along with the PHP: strlen function to count the number of characters. I would also add a note under Field Instructions so the user knows the limit before submitting.

    Assuming you want a minimum of ten characters:

    add_filter('acf/validate_value/name=validate_this_image', 'my_acf_validate_value', 10, 4);
     
    function my_acf_validate_value( $valid, $value, $field, $input ){
     
    	// bail early if value is already invalid
    	if( !$valid ) {
     
    		return $valid;
     
    	}
     
     
    	// load data
    	
    	$my_text_field = get_field('text_field');
    	
    	if ( strlen($my_text_field) < 10 ) {
    		
    		$valid = 'Must be a minimum of 10 characters.';
    	} 
    	
     
    	// return
    	return $valid;
     
    }