php 电子邮件和密码的 CodeIgniter 验证规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30707412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter Validation Rules for Email and Password
提问by Mohamed Ebrahim
I was used to coding application with CodeIgniter and I'm a total newbie. I just started to learn CodeIgniter 3.0, and and reached to validation rules.
我习惯于使用 CodeIgniter 编写应用程序,我完全是个新手。我刚开始学习 CodeIgniter 3.0,并达到了验证规则。
I notice that xss_clean
is now gone from the validation class, so what rules should I use in the validation of email and password? Using just trim
, valid_email
, and required is enough for security?
我注意到xss_clean
现在从验证类中消失了,那么我应该在验证电子邮件和密码时使用什么规则?只需使用trim
,valid_email
以及所需的安全性是否足够?
Sorry if that question has been asked, but I searched around and I see old topics where people is using xss_clean
.
抱歉,如果有人问过这个问题,但我四处搜索,看到人们使用xss_clean
.
回答by Deep Kakkar
Include Email Helper:
包括电子邮件助手:
$this->load->helper('email');
$this->load->helper('email');
For Email:
对于电子邮件:
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
Or you can also use PHP Filter for email validation as
或者您也可以使用 PHP 过滤器进行电子邮件验证
filter_var($email, FILTER_VALIDATE_EMAIL);
filter_var($email, FILTER_VALIDATE_EMAIL);
For Password Expression
对于密码表达式
public function chk_password_expression($str)
{
if (1 !== preg_match("/^.*(?=.{6,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $str))
{
$this->form_validation->set_message('chk_password_expression', '%s must be at least 6 characters and must contain at least one lower case letter, one upper case letter and one digit');
return FALSE;
}
else
{
return TRUE;
}
}
To call the function you should use:
要调用该函数,您应该使用:
$this->form_validation->set_rules( 'password', 'Password', 'trim|required|min_length[6]|max_length[15]|callback_chk_password_expression');
Note: chk_password_expression should be in same class controller or in parent class. Email helper should be included as $this->load->helper('email');
注意:chk_password_expression 应该在同一个类控制器或父类中。电子邮件助手应包含为 $this->load->helper('email');