how to use regular expressions in yii

2020-03-26 12:27发布

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

Thank you

标签: regex yii
1条回答
ら.Afraid
2楼-- · 2020-03-26 12:51

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.',
        ),
    );
}

Note: Add your regular expression as value for 'pattern' key.

查看更多
登录 后发表回答