0

I am using codeigniter form validation to validate user data when creating new user. I want to add some sort of password criteria for example password must have at least one capital letter, a number, and one of !, @, #, $ etc) and there have to be 6-25 characters.

This is the array that I am using for validation rules:

$config = array(
           array(
                 'field'   => 'title',
                 'label'   => 'Title',
                 'rules'   => 'trim|required'
              ),
            array(
                 'field'   => 'firstname',
                 'label'   => 'First Name',
                 'rules'   => 'trim|required|min_length[2]|max_length[100]|xss_clean'
              ),
           array(
                 'field'   => 'lastname',
                 'label'   => 'Last Name',
                 'rules'   => 'trim|required|min_length[2]|max_length[100]|xss_clean'
              ),
           array(
                 'field'   => 'phone',
                 'label'   => 'Telephone',
                 'rules'   => 'trim|required|intiger|min_length[11]|max_length[11]|xss_clean'
              ),
           array(
                 'field'   => 'email',
                 'label'   => 'Email',
                 'rules'   => 'trim|required|valid_email|is_unique[ci_user.email]|matches[conf_email]|xss_clean'
              ),
           array(
                 'field'   => 'conf_email',
                 'label'   => 'Confirm Email',
                 'rules'   => 'trim|required|valid_email|xss_clean'
              ),
           array(
                 'field'   => 'password',
                 'label'   => 'Password',
                 'rules'   => 'trim|required|min_length[6]|max_length[25]|matches[conf_password]|xss_clean'
              ),
           array(
                 'field'   => 'conf_password',
                 'label'   => 'Confirm Password',
                 'rules'   => 'trim|required|xss_clean'
              ));

Can someone please guide me on how to achieve what I need. Thank you

user4676307
  • 409
  • 8
  • 22
  • perform a callback. Check this out: http://stackoverflow.com/questions/10525944/codeigniter-require-letters-and-numbers-in-password – CodeGodie Sep 10 '15 at 12:57
  • Check this [link](http://stackoverflow.com/questions/10525944/codeigniter-require-letters-and-numbers-in-password) and [link1](http://stackoverflow.com/questions/9477906/password-must-be-8-characters-including-1-uppercase-letter-1-special-character)] – Saty Sep 10 '15 at 13:00
  • Hi, I used the link and added ''trim|required|min_length[6]|max_length[25]|matches[conf_password]|callback_password_check|xss_clean'' to my password rule and also created password_check method but it is not working – user4676307 Sep 10 '15 at 13:30

2 Answers2

1

you can setup call back function to check password strong validation. and call this function callback_is_password_strong in this line of your code.

array(
    'field'   => 'password',
    'label'   => 'Password',
    'rules'   => 'trim|required|min_length[6]|max_length[25]|matches[conf_password]|xss_clean|callback_is_password_strong'
),

and if you look this function will return true or false and password array rule key is validated only when it returns true

public function is_password_strong($password)
{
   if (preg_match('#[0-9]#', $password) && preg_match('#[a-zA-Z]#', $password)) {
     return TRUE;
   }
   return FALSE;
}
Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47
  • Hi I have already tried that but for some reason it doesn't do anything. So it is like the code within callback just gets ignored or the callback function just doesn't get called at all. – user4676307 Sep 10 '15 at 14:32
  • I have added this to my controller and have not created a new library that extends Codeigniter Form Validation. Do I need to do this? – user4676307 Sep 10 '15 at 14:36
  • [Custom validation Rules](https://arjunphp.com/custom-validation-rules-codeigniter/) – Kaleem Ullah Sep 10 '15 at 14:57
  • [How to use custom form validation in codeigniter](http://tutsnare.com/custom-form-validation-in-codeigniter/) – Kaleem Ullah Sep 10 '15 at 15:08
0

Using callback function

public function check_strong_password($str)
    {
       if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) {
         return TRUE;
       }
       $this->form_validation->set_message('check_strong_password', 'The password field must be contains at least one letter and one digit.');
       return FALSE;
    }

Usage

$this->form_validation->set_rules('adminPassword', 'password', 'required|min_length[8]|max_length[25]|callback_check_strong_password');
Billu
  • 2,733
  • 26
  • 47