how to use regular expressions in yii

regex, yii

Solution

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`.

Problem

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

Original source