Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use regular expressions in yii

Tags:

regex

yii

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

like image 505
user1908466 Avatar asked Jan 27 '26 02:01

user1908466


1 Answers

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.

like image 103
secretlm Avatar answered Jan 30 '26 15:01

secretlm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!