Codeigniter表单验证在数组中设置强密码验证规则 [英] Codeigniter Form Validation Setting Strong Password Validation Rule In an Array

查看:315
本文介绍了Codeigniter表单验证在数组中设置强密码验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用codeigniter表单验证来验证创建新用户时的用户数据。我想添加某种密码标准,例如密码必须有
至少一个大写字母,数字和!,@,#,$等)之一,并且必须有6-25个字符。 / p>

这是我用于验证规则的数组:

  $ config = array(
array(
'field'=>'title',
'label'=>'Title',
'rules'=>修改|必需'
),
数组(
'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'=>'确认电子邮件',
'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'=>'确认密码',
'rules'=> 'trim | required | xss_clean'
));

有人可以指导我如何实现我需要的。谢谢

解决方案

您可以设置回拨功能来检查密码强力验证。并在代码的这一行中调用此函数 callback_is_password_strong

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

你看这个函数将返回true或false和密码数组规则键只有当它返回true时才被验证

  public function is_password_strong密码)
{
if(preg_match('#[0-9]#',$ password)&&& preg_match('#[a-zA-Z]#',$ password)) {
return TRUE;
}
return FALSE;
}


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

解决方案

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

这篇关于Codeigniter表单验证在数组中设置强密码验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆