![](/img/trans.png)
[英]Invert case of all letters in a string (uppercase to lowercase and lowercase to uppercase)
[英]CodeIgniter strong password policy with compulsory uppercase/lowercase letters and digits
我想要求用戶選擇一個至少包含一個大寫字母和一個數字的強密碼。 使用CodeIgniter驗證表單時如何執行此策略?
擴展默認的Form_validation
類,並為您的自定義密碼驗證創建一個自定義驗證規則。
要擴展默認類,您需要創建一個MY_Form_validation
類並將其放入您的application/libraries/MY_Form_validation.php
。 這是一個例子:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Extension of CI's default form validation library.
*
* @see system/libraries/Form_validation.php
*/
class MY_Form_validation extends CI_Form_validation {
/**
* Custom password validation rule.
*
* @param string $pass Password to check.
*
* @return bool
*/
public function check_pass($pass)
{
// It's a good practice to make sure each validation rule does
// its own job. So we decide that this callback does not check
// for the password field being required. If we need so, we just
// prepend the "required" rule. For example: "required|min_length[8]|check_pass"
//
// So we just let it go if the value is empty:
if (empty($pass))
{
return TRUE;
}
// This sets the error message for your custom validation
// rule. %s will be replaced with the field name if needed.
$this->set_message('check_pass', 'Password needs to have at least one uppercase letter and a number.');
// The regex looks ahead for at least one lowercase letter,
// one uppercase letter and a number. IT'S NOT TESTED THOUGH.
return (bool) preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/', $pass);
}
}
有了此自定義類,設置規則時, check_pass
驗證規則將對您的控制器可用。
如果您懶於添加此自定義類,或者已經在其他地方實現了驗證功能,則可能要使用自定義驗證回調, callback_
是將callback_
放在現有函數的名稱之前,並將它們用作驗證規則。 有關更多信息,請參見驗證回調 。
我不建議使用后一種方法,因為它會混淆驗證規則所在的位置。 在某些情況下,我們必須使用不在驗證類中的自定義回調,在這些情況之外(不是您的情況),所有規則最好都在您的自定義類中。
PS還請考慮以下事項:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.