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

查看:672
本文介绍了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] + $ /]'

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

除了,这实际上不会在codeigniter中工作明显因为它如何处理验证规则中的括号,因此您需要使用回调( see here )。

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;
  }
}

然后设置规则以调用函数: / p>

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表单验证< a>有关扩展 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文件中找到,第115行从2.1.3开始。

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天全站免登陆