Skip Navigation

[Resolved] How to set password minimum length

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to enforce a minimum length for User passwords in Forms.

Solution: You can use the cred_form_validate API to apply back-end validation to password fields.

// require minimum password length in User Forms
add_filter( 'cred_form_validate', 'validate_password_length', 10, 2 );
function validate_password_length( $error_fields, $form_data ) {
  $forms = array( 1234, 5678 );
  list($fields,$errors)=$error_fields;
 
  if( in_array( $form_data['id'], $forms ) ){
    $p = isset($fields['user_pass']['value']) ? $fields['user_pass']['value'] : '';
    if( strlen($p) < 6 ){
      $errors['user_pass'] = __('Password must include a minimum of 6 characters');
    }
  }
  return array( $fields, $errors );
}

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.36 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Tiit Sau 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#955565

I wish to set password length in user forms let say 6 characters as minimum.
Can't find anything in documentation about it.
By default Toolset Forms tells 1 character password is ok, but we all know it is not.
Some Javascript have to be added i quess, but a clear example would be nice.

#955613

Hi, I don't have a cut-and-paste JavaScript solution for you, since there is no Forms JavaScript API. It is possible to add Form validation using the cred_form_validate PHP API, so I can show you how to add backend password length validation. Error messages will be displayed after a Form is submitted with invalid information.

// require minimum password length in User Forms
add_filter( 'cred_form_validate', 'validate_password_length', 10, 2 );
function validate_password_length( $error_fields, $form_data ) {
  $forms = array( 1234, 5678 );
  list($fields,$errors)=$error_fields;

  if( in_array( $form_data['id'], $forms ) ){
    $p = isset($fields['user_pass']['value']) ? $fields['user_pass']['value'] : '';
    if( strlen($p) < 6 ){
      $errors['user_pass'] = __('Password must include a minimum of 6 characters');
    }
  }
  return array( $fields, $errors );
}

Replace 1234, 5678 with a comma-separated list of Form IDs where you want to apply this validation. You can modify the "6" in the "if" and in the error message to change the required minimum length.

#955626

Nice job, works just like planned.

Thanks Christian!