Skip Navigation

[Resolved] Split: User sign-up not working – validate user form

This support ticket is created 5 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by geoffD 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1392361

Hi Minesh..

On the user register form that you're working on, as well as logging the user in I also need to add some validation to make sure the user includes their first and last name.. The email seems to be required by default...

Also I need to add validation to the password so that it is at least 8 characters long and uses both letters and numbers...is this possible?

I shall look forward to hearing back from you

Thanks as always for all of your help and support

Best regards

Geoff

#1392381

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Toolset form's filter hook "cred_filter_field_before_add_to_form" to add both first_name and last_name fields as required.

I've added the following code to the "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter('cred_filter_field_before_add_to_form', 'required_user_fields_func', 10, 2);
function required_user_fields_func($field, $computed_values){
  if(in_array($field['id'], array('first_name', 'last_name'))){
    $field['data'] = is_array($field['data']) ? $field['data'] : array();
    $field['data']['validate']['required'] = array (
      'active' => 1,
      'value' => 1,
      'message' => 'This field is required'
    );
  }
  return $field;
}

If you can check now: hidden link

Both first_name and last_name fields are required.

#1392395

That's great Minesh...but how would I add a validation for the password to be at least 8 characters long and a mix of letters and numbers..

Best regards

Geoff

#1392399

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

but how would I add a validation for the password to be at least 8 characters long and a mix of letters and numbers..
==>
I was working on that issue only 🙂

I've added the following code to the "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'cred_form_validate', 'func_check_password_len_with_numchar', 10, 2 );
function func_check_password_len_with_numchar( $error_fields, $form_data ) {
  $forms = array( 1562);
  list($fields,$errors)=$error_fields;
 
  if( in_array( $form_data['id'], $forms ) ){
    $pass = isset($fields['user_pass']['value']) ? $fields['user_pass']['value'] : '';
    $has_numb_char = preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $pass);
    if( strlen($pass) < 8 and !$has_numb_char ){
      $errors['user_pass'] = __('Password must include a minimum of 8 characters');
    }
  }
  return array( $fields, $errors );
}

Now, it validates the password as you per your requirement.

#1392453

Hi Minesh

I have tried a couple of different passwords and each time it brings up the validation message and won't send the form..

I've tried these passwords:
GURUguru
GURUguru17

Can you let me know exactly what the validation is please..

Gest regards

Geoff

#1392973

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've adjusted the code as given under so it will display different errors - the password will only allow letters and numbers and password must be greater than 8 characters.

add_filter( 'cred_form_validate', 'func_check_password_len_with_numchar', 10, 2 );
function func_check_password_len_with_numchar( $error_fields, $form_data ) {
  $forms = array( 1562);
  list($fields,$errors)=$error_fields;
 
  if( in_array( $form_data['id'], $forms ) ){
    
   $pass = isset($_POST['user_pass']) ? $_POST['user_pass'] : '';
    
   if( strlen($pass) < 8 ){
      $errors['user_pass'] = __('Password must include a minimum of 8 characters');
    }else if(!ctype_alnum($pass)){
      $errors['user_pass'] = __('Password must include letters and numbers');
    }
  }
  return array( $fields, $errors );
}
#1393285

Hi Minesh

This is great and all works fine however I want the user to use a mix of BOTH numbers and letters..so the user has to include at least ONE number e.g.

abcdefgh - this is wrong
abcdefg4 - this is correct

Hope that makes sense

Best regards as always

Geoff

#1393959

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

abcdefgh - this is wrong
abcdefg4 - this is correct
==>
Yes - that validation already added. Please check that and feel free to close this ticket.

#1394191

Hi Minesh

It still lets me create an account with the password 'abcdefgh' - This is incorrect as it is does not include numbers..I need the password to use BOTH numbers and letters..

Also Minesh my original ticket re the logging in after sign up seems to have gone cold...are you still working on this...just checking not pestering..I know you're busy!..

Thank you as always

Best regards

Geoff

#1394201

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry - Yesterday - I was on a half-day holiday due to my bad health. I also replied on your original ticket as well.

Can you please check now: hidden link

I've adjusted the code as given under to validatae the passwords. I made few test and it works.

add_filter( 'cred_form_validate', 'func_check_password_len_with_numchar', 10, 2 );
function func_check_password_len_with_numchar( $error_fields, $form_data ) {
  $forms = array( 1562);
  list($fields,$errors)=$error_fields;
 
  if( in_array( $form_data['id'], $forms ) ){
    
   $pass = isset($_POST['user_pass']) ? $_POST['user_pass'] : '';
    
  $charnum_flag = preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $pass);
   
   if( strlen($pass) < 8 ){
      $errors['user_pass'] = __('Password must include a minimum of 8 characters');
    }else if(!$charnum_flag){
      $errors['user_pass'] = __('Password must include letters and numbers');
    }
  }
  return array( $fields, $errors );
}

Can you please confirm it works at your end as well.

#1394251

My issue is resolved now. Thank you!