Codeigniter Form Validation Setting Strong Password Validation Rule In an Array

无人久伴 提交于 2019-12-25 12:05:35

问题


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


回答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;
}


来源:https://stackoverflow.com/questions/32502446/codeigniter-form-validation-setting-strong-password-validation-rule-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!