CodeIgniter 表单验证 - 如何使其不区分大小写

发布于 2024-12-17 06:44:39 字数 652 浏览 3 评论 0原文

我正在使用 Code Igniter 2,如果用户输入相同的电子邮件地址但在不同的情况下,以下代码不会验证,例如: [电子邮件受保护][电子邮件受保护] 我希望即使用户使用不同的情况,也可以验证它是否是同一封电子邮件。

$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim');

if ($this->form_validation->run() == FALSE)
{

    $this->load->view('login_view', $data);
}
else
{

I am using Code Igniter 2, and the following code doesn't validate in case the user entered the same email address but in different cases, for example: [email protected] and [email protected]
I want this to validate if it's the same email even if the user used different cases.

$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim');

if ($this->form_validation->run() == FALSE)
{

    $this->load->view('login_view', $data);
}
else
{

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

書生途 2024-12-24 06:44:39

任何接受一个参数的 PHP 函数都可以在验证类中使用。

来自手册

任何接受一个参数的本机 PHP 函数都可以用作
规则,如 htmlspecialchars、trim、MD5 等

这意味着您只需修改验证规则即可

$this->form_validation->set_rules('email', 'Email', 'required|valid_email|strtolower|trim');

注意 的使用规则中的 strtolower() 函数。

Any PHP function which accepts one parameter can be used in the validation class.

From the manual

Any native PHP function that accepts one parameter can be used as a
rule, like htmlspecialchars, trim, MD5, etc.

Which means you can just amend your validation rule to

$this->form_validation->set_rules('email', 'Email', 'required|valid_email|strtolower|trim');

Note the use of the strtolower() function in the rules.

猫七 2024-12-24 06:44:39

您需要在控制器内创建一个回调函数:

function valid_case_insensitive_email($str){
    $str = strtolower($str)
    return $this->form_validation->valid_email($str)
}

然后将 set_rules 修改为此

$this->form_validation->set_rules('email', 'Email', 'required|callback_valid_case_insensitive_email|trim');

或者您可以扩展 form_validation 类并覆盖 valid_email 函数。

You need to create a callback function inside your controller:

function valid_case_insensitive_email($str){
    $str = strtolower($str)
    return $this->form_validation->valid_email($str)
}

Then modify the set_rules to this

$this->form_validation->set_rules('email', 'Email', 'required|callback_valid_case_insensitive_email|trim');

Alternatively you could extend the form_validation class and overwrite the valid_email function.

人心善变 2024-12-24 06:44:39

这样做:

$this->form_validation->set_rules('email', 'Email', 'required|callback__check_email');

//then the callback function

function _check_email() {
  $email = strtolower($this->input->post("email");
  $email = trim($email);
  return $this->form_validation->valid_email($email);
}

希望有帮助

Do this as:

$this->form_validation->set_rules('email', 'Email', 'required|callback__check_email');

//then the callback function

function _check_email() {
  $email = strtolower($this->input->post("email");
  $email = trim($email);
  return $this->form_validation->valid_email($email);
}

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文