duanlang1531 2019-01-24 03:18
浏览 83

使用尊重验证进行密码验证

I've currently got a regular expression set up for validating my passwords:

/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W).{6,}$/

This checks for at least 1 uppercase and lowercase character, 1 digit, 1 special character/non-word character and minimum of 6 characters.

I'm now porting all of my validation rules over to Respect Validation. I've got most of it set up nicely but having difficulty with password validation.

I'm aware of the regex method:

Validator::regex('/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W).{6,}$/')->setName('Password');

But this returns an error message like this:

Password must validate against "/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W).{6,}$/"

I'm wanting to chain methods together so each rule is set in the validation chain. So if it's a digit you are missing it returns something like "Password must contain at least 1 digit", if you're missing an uppercase character it returns "Password must contain at least 1 uppercase character" and so on.

I thought it might be done using the allOff method, whereby you can set individual rules that will pass globally if all inner rules pass.

As a test, I tried to detect if the string contains a digit and an alpha character like this:

Validator::allOf(Validator::digit(), Validator::alpha())->setName('Password');

The results:

$password = 'test'; // Password must contain only digits (0-9)
$password = '1234'; // Password must contain only letters (a-z)
$password = 'test1234' // Password must contain only digits (0-9)

Looking at it now, it makes perfect sense why I got those errors, but I can't see how else to do this without the regex method. Is what I'm after possible is it just best to use the regex method in this case, I've gone through the rule list a few times but I've probably missed the one I'm after as there is quite a few.

Any help is appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题