Support

Account

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

Solving

Minimum number of characters in a text field

  • Hello I would like to add fill a minimum number of characters in a text field. There exist a function?

    Thank you for your help

  • 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;
     
    }
    
  • Hey,
    I’ve now tried this solution and it work’s! thanks! even though, I have two things I’m trying to achieve with no success:
    1. Is it possible to do this validation to the first field in a repeater? only to the first.
    2. Is it possible to apply this validation on the wp core editor?

    Thanks,
    Itamar

  • Actually now I see that the soution doesn’t reallt work for me. I’m trying to apply this validation on a wysiwyg field and the strlen always returns 0 characters.

    Itamar

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Minimum number of characters in a text field’ is closed to new replies.