简体   繁体   English

regex_match 的表单验证规则

[英]Form validation rules for regex_match

I'm trying to figure out what I'm doing wrong with this validation rule because its saying this error.我试图弄清楚我在这个验证规则上做错了什么,因为它说的是这个错误。

Validation rule:验证规则:

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

Error:错误:

Severity: Warning严重性:警告
Message: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash消息:preg_match() [function.preg-match]:分隔符不能是字母数字或反斜杠

First, you need to fix your pattern.首先,您需要修复您的模式。 Your pattern right now is a-z0-9你现在的模式是a-z0-9

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/ (如果你在 Codeigniter 中使用反斜杠分隔符有问题,你有很多其他的选择:请参阅PCRE 分隔符的手册页

Now you have a pattern that literally matches a string with a-z0-9 someplace in it.现在你有一个模式,它在字面上匹配一个带有a-z0-9的字符串。 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]/请注意,如果您尝试将其作为字符类进行匹配,则需要再次将其括在方括号中以使其如此,因此/[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]+$/

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

So your third parameter would end up looking like:所以,你的第三个参数最终看起来像:

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

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 ).除了这实际上在 codeigniter 中显然不起作用(截至 2011 年 3 月,无论如何都是这种情况),由于它如何处理验证规则中的括号,因此您需要使用回调(请参阅此处)。

Using a callback :使用回调

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');

Notes: the function is named with a leading underscore to prevent url access if you create your callback function within the controller.注意:如果您在控制器中创建回调函数,则该函数使用前导下划线命名以防止 url 访问。 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.通过创建一个扩展CI_Form_validation类的类,有一种替代方法可以做到这一点,但这似乎是一个更复杂的解决方案。 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 .如果您使用前导下划线命名函数,请记住,您必须将其包含在规则中,这会导致callback__usernameRegex总共有两个下划线:一个用于callback_的前缀,第二个用于函数名称_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 .有关扩展CI_Form_validation的详细说明,请参阅将自定义回调添加到 Codeigniter 表单验证的链接答案。 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 .请注意,如果您扩展表单验证,则当您在set_rules引用它时,您不需要使用callback_附加您的函数。

You can also set the message for your new callback:您还可以为新回调设置消息:

$this->form_validation->set_message('usernameRegex', 'The %s field must only contain lowercase letters and/or numbers');


The alternative route is to modify Codeigniter's own pattern match used on the form_validation rules.另一种方法是修改 Codeigniter 自己在 form_validation 规则上使用的模式匹配。 The link to Ellislab's forum describes the issue and then a potential way to work around it further down the thread . Ellislab 论坛的链接描述了这个问题,然后是在线程中进一步解决它的潜在方法。 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 代码和指定的修复程序来看,他们提供的建议解决方案可能会破坏在规则中使用数组:使用递归匹配替换 Codeigniter 函数中的一个更好的正则表达式可以解决这两个问题,但可能会遇到性能问题规则。

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. Codeigniter 对 form_validation 规则的模式匹配可以在它的 system/libraries/Form_validation.php 文件中找到,在 2.1.3 的第 115 行。

使用以下代码在我结束时工作正常

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

php 的 preg_match 想要你的正则表达式被一个非字母数字字符包围(通常每个人都使用 /),将你的 setRules() 更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM