regex_match 的表单验证规则 [英] Form validation rules for regex_match

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

问题描述

我试图找出我在这条验证规则上做错了什么,因为它说的是这个错误.

I'm trying to figure out what I'm doing wrong with this validation rule because its saying this error.

验证规则:

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|regex_match[a-z0-9]');

错误:

严重性:警告
消息:preg_match() [function.preg-match]:分隔符不能是字母数字或反斜杠

Severity: Warning
Message: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

推荐答案

首先,您需要修复您的模式.你现在的模式是 a-z0-9

First, you need to fix your pattern. Your pattern right now is a-z0-9

它需要有一个分隔符来包围它,比如 /a-z0-9/(如果你在 Codeigniter 中使用反斜杠分隔符有问题,你有很多其他的选择:见 有关 PCRE 分隔符的手册页)

It needs to have a delimiter enclosing it, so something like /a-z0-9/ (If you have issues in Codeigniter using backslash delimiters, you have a number of other options: see The manual page on PCRE delimiters)

现在你有一个模式,它在字面上匹配一个带有 a-z0-9 的字符串.请注意,如果您尝试将其作为字符类进行匹配,则需要再次将其括在方括号中以使其如此,因此 /[a-z0-9]/

Now you have a pattern that literally matches a string with a-z0-9 someplace in it. Note that if you were trying to match that as a character class, you would need to again encase it in brackets to make it that, so /[a-z0-9]/

现在我将假设您实际上想要检查整个字符串是否为(小写)字母数字,而不仅仅是它有一个与之匹配的字符.在这种情况下,您需要一个前锚点、要匹配的字符类、一个重复修饰符(我假设您不想匹配空字符串并将其设为一个或多个),然后是一个锚点来指定匹配必须延伸到最后:

Now I'm going to assume that you actually wanted to check that the entire string was (lowercase) alphanumeric, not just that it had a character matching that. In which case you need a front anchor, your character class to match, a repetition modifier (I'll assume you don't want to match an empty string and make it one or more), and then an anchor to specify that the match must extend to the end:

/^[a-z0-9]+$/

您甚至可以通过用大括号代替加号并提供最小值和最大值来设置最小长度(保留最大值以允许其长度超过最小值)...例如 /^[a-z0-9]{3,}$

You could even set a minimum length by substituting braces for the plus and providing a minimum, maximum (leave the max out to allow it to be any length over min)... for example /^[a-z0-9]{3,}$

所以你的第三个参数最终看起来像:

So your third parameter would end up looking like:

'trim|required|xss_clean|regex_match[/^[a-z0-9]+$/]'

除了这实际上在 codeigniter 中显然不起作用(截至 2011 年 3 月,无论如何都是这种情况),因为它如何处理验证规则中的括号,所以你需要使用回调(参见此处).

Except that this actually won't work in codeigniter apparently (as of March 2011 this was the case anyway), due to how it handles brackets inside the validation rules, so you'll need to use a callback (see here).

您可以在控制器(或其他适当的范围内)中放置如下所示的函数:

You would place a function like the following in your controller (or of otherwise appropriate scope):

public function _usernameRegex($userName) {
  if (preg_match('/^[a-z0-9]+$/', $userName ) ) 
  {
    return TRUE;
  } 
  else 
  {
    return FALSE;
  }
}

然后你设置规则来调用你的函数:

You then set the rule to call your function:

set_rules('username', 'Username', 'trim|required|xss_clean|callback__usernameRegex');

注意:如果您在控制器中创建回调函数,则该函数使用前导下划线命名以防止 url 访问.有一种替代方法可以通过创建一个扩展 CI_Form_validation 类的类来做到这一点,但这似乎是一个更复杂的解决方案.如果您使用前导下划线命名函数,请记住您必须然后将其包含在您的规则中,这导致 callback__usernameRegex 总共有两个下划线:一个用于前缀 callback_ 和第二个函数的名称,_usernameRegex.这可能非常简单,不需要向您指出,但在我看来,这似乎很容易被遗漏.请参阅向 Codeigniter 表单验证添加自定义回调 有关扩展 CI_Form_validation 的详细说明.请注意,如果您扩展表单验证,当您在 set_rules 中引用它时,您不需要使用 callback_ 附加您的函数.

Notes: the function is named with a leading underscore to prevent url access if you create your callback function within the controller. There is an alternative way to do this by creating a class which extends the CI_Form_validation class, but that seemed like a more complicated solution. If you name the function with a leading underscore, remember that you must then include that in your rules, which results in callback__usernameRegex having two underscores total: one for the prefix of callback_ and the second for the name of the function, _usernameRegex. This may be utterly straightforward and not need pointing out to you, but to me it seemed like something that could easily be missed. See the linked answer for Adding custom callback to Codeigniter Form Validation for a detailed explanation on extending CI_Form_validation. Note that if you extend the form validation, you do not need to append your function with callback_ when you reference it in set_rules.

您还可以为新回调设置消息:

You can also set the message for your new callback:

$this->form_validation->set_message('usernameRegex', '%s 字段只能包含小写字母和/或数字');

另一种方法是修改 Codeigniter 自己在 form_validation 规则上使用的模式匹配.Ellislab 论坛的链接描述了该问题,然后是解决该问题的潜在方法深入讨论.从查看 Codeigniter 代码和指定的修复程序来看,他们提供的建议解决方案可能会破坏在规则中使用数组:使用递归匹配替换 Codeigniter 函数中的一个更好的正则表达式可以解决这两个问题,但可能会遇到性能问题规则.

The alternative route is to modify Codeigniter's own pattern match used on the form_validation rules. The link to Ellislab's forum describes the issue and then a potential way to work around it further down the thread. From glancing at the Codeigniter code and the fix specified, the proposed solution they provided may break using arrays in rules: a better regex to replace the one in the Codeigniter function by using recursive matching would solve both, but might run into performance issues on heavier rules.

Codeigniter 对 form_validation 规则的模式匹配可以在它的 system/libraries/Form_validation.php 文件中找到,在 2.1.3 的第 115 行.

Codeigniter's pattern matching for form_validation rules is found in its system/libraries/Form_validation.php file, on line 115 as of 2.1.3.

这篇关于regex_match 的表单验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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