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);
Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
asked Sep 15, 2016 at 15:24
2 Answers 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');
}
answered Sep 15, 2016 at 15:30
-
3rd parameter must be an array (see my answer) ;)Raphael at Digital Pianism– Raphael at Digital Pianism2016年09月15日 15:31:19 +00:00Commented Sep 15, 2016 at 15:31
-
@RaphaelatDigitalPianism, yep but it must have the key
patternin it. See my update answer :) github.com/OpenMage/magento-mirror/blob/magento-1.9/lib/Zend/…. And it works with a simple string alsoMarius– Marius2016年09月15日 15:37:40 +00:00Commented Sep 15, 2016 at 15:37 -
Sure about the key
pattern? github.com/OpenMage/magento-mirror/blob/1.9.2.4/app/code/core/…Raphael at Digital Pianism– Raphael at Digital Pianism2016年09月15日 15:39:01 +00:00Commented Sep 15, 2016 at 15:39 -
Oh ok yeah here we go again posting the same answers ;)Raphael at Digital Pianism– Raphael at Digital Pianism2016年09月15日 15:39:56 +00:00Commented Sep 15, 2016 at 15:39
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}/"))) {
}
answered Sep 15, 2016 at 15:28
Raphael at Digital Pianism Raphael at Digital Pianism
70.8k37 gold badges192 silver badges357 bronze badges
-
Thank you @RaphaelatDigitalPianism I will test the code.Elvis– Elvis2016年09月15日 16:11:27 +00:00Commented Sep 15, 2016 at 16:11
Explore related questions
See similar questions with these tags.
default