如何在yii中使用正则表达式 [英] how to use regular expressions in yii
问题描述
我需要验证
username
和password
的格式是否正确.就我而言,6-14 个字母\数字或*".由于规则
可能会改变,我想使用易于维护的东西.
我在网上看了关于 CRegularExpressionValidator
的解释,但我就是不明白.有人可以告诉我我应该做什么吗?或者甚至只是给我一个包含所有步骤的简单示例的链接
I need to validate
that a username
and password
is in the right format. In my case 6-14 letters\digits or '*'. Since the rules
may change I want to use something that is easy to maintain.
I have looked online on the explanation about CRegularExpressionValidator
but I just don't get it.
Can someone please tell me what exactly I should do? or even just give me a link to a simple example that cover all the steps
谢谢
推荐答案
您可以尝试使用 match
(CRegularExpressionValidator) 规则来验证属性值与特定的正则表达式如:
You can try to use match
(CRegularExpressionValidator) rule to validate the attribute value with the specific regular expression like:
public function rules() {
return array(
array('username, password', 'required'),
array(
'username, password',
'match', 'pattern' => '/^[\*a-zA-Z0-9]{6,14}$/',
'message' => 'Invalid characters in username or password.',
),
);
}
注意:添加正则表达式作为'pattern'键的值
.
这篇关于如何在yii中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!