Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
up vote 1 down vote accepted
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');
}
share|improve this answer
    
3rd parameter must be an array (see my answer) ;) – Raphael at Digital Pianism yesterday
    
@RaphaelatDigitalPianism, yep but it must have the key pattern in it. See my update answer :) github.com/OpenMage/magento-mirror/blob/magento-1.9/lib/Zend‌​/…. And it works with a simple string also – Marius yesterday
    
    
Oh ok yeah here we go again posting the same answers ;) – Raphael at Digital Pianism yesterday
    
Thank you @Marius I will test the code. – Elvis yesterday

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}/"))) {
}
share|improve this answer
    
Thank you @RaphaelatDigitalPianism I will test the code. – Elvis yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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