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;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.