1

I would like to place a RegEx Code in PHP code to validate a password. Example below of current code:

if (strlen($password) && !Zend_Validate::is($password, 'StringLength', array(6))) {
    $errors[] = Mage::helper('customer')->__('The minimum password length is %s', 6);
}

I would like to replace the array(6) with this regular expression with preg_match:

preg_match("/(?=.*[\d])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&])[0-9a-zA-Z!@#$%^&]{10,16}/", $input_line, $output_array);

2 Answers 2

2
if (strlen($password) && !Zend_Validate::is($password, 'Regex', array('/(?=.*[\d])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&])[0-9a-zA-Z!@#$%^&]{10,16}/')) {
    $errors[] = Mage::helper('customer')->__('your error message here');
}
4
3

I reckon you should use Zend_Validate_Regex in that case.

You could do:

$validate = new Zend_Validate_Regex("/(?=.*[\d])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&])[0-9a-zA-Z!@#$%^&]{10,16}/");

if (strlen($password) && !$validate->isValid($password)) {
}

Alternative

You can also do:

if (strlen($password) && !Zend_Validate::is($password, 'Regex', array("/(?=.*[\d])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&])[0-9a-zA-Z!@#$%^&]{10,16}/"))) {
}
1
  • Thank you @RaphaelatDigitalPianism I will test the code.
    – Elvis
    Commented Sep 15, 2016 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.