我的表单中的密码字段的一个条件是它应该有一个大写字母,一个小写字母和一个数字。为了实现这一点,我创建了我的表单元素,如下所示。
$password = $this->createElement('password', 'password');
$password->setAttrib('id', 'password')
->setAttrib('class', 'form-control')
->setAttrib('placeholder', 'Password')
->setLabel('Password'.$this->indicatorIcon('required'))
->setRequired(true)
->addValidator('NotEmpty', true, array('messages' => $this->indicatorIcon('error').'Password can\'t be left blank'))
->addValidator('StringLength', true, array('min' => 8, 'max' => 30, 'messages' => $this->indicatorIcon('error').'Password should be 8 - 30 characters long'))
->addValidator('Regex', true, array('pattern' => '[A-Z]', 'messages' => $this->indicatorIcon('error').'Password should contain at least one upper case letter'))
->addValidator('Regex', true, array('pattern' => '[a-z]', 'messages' => $this->indicatorIcon('error').'Password should contain at least one lower case letter'))
->addValidator('Regex', true, array('pattern' => '/\d/', 'messages' => $this->indicatorIcon('error').'Password should contain at least one digit'))
->addFilter('StringTrim');
但我只能验证小写字母或大写字母。我添加这些验证器的顺序会覆盖另一个。带数字的那个工作正常。
有没有办法解决这个问题?我已经尝试了许多其他正则表达式,但他们并没有奏效。任何人都可以帮助我使这个代码工作或建议一个替代解决方案吗?
答案 0 :(得分:0)
你是低级和高级的正则表达式。如果你试试这个,我认为它会起作用。
$password = $this->createElement('password', 'password');
$password->setAttrib('id', 'password')
->setAttrib('class', 'form-control')
->setAttrib('placeholder', 'Password')
->setLabel('Password'.$this->indicatorIcon('required'))
->setRequired(true)
->addValidator('NotEmpty', true, array('messages' => $this->indicatorIcon('error').'Password can\'t be left blank'))
->addValidator('StringLength', true, array('min' => 8, 'max' => 30, 'messages' => $this->indicatorIcon('error').'Password should be 8 - 30 characters long'))
->addValidator('Regex', true, array('pattern' => '(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+', 'messages' => $this->indicatorIcon('error').'Password should contain at least one upper case letter and one lower case letter minimum and should contain at least one digit'))
->addFilter('StringTrim');
我只将三种模式连接在一起。