Angular 如何验证密码是否有多个大写字母
我使用的是一个自定义验证器函数,它使用正则表达式来确定匹配项Angular 如何验证密码是否有多个大写字母,angular,typescript,Angular,Typescript,我使用的是一个自定义验证器函数,它使用正则表达式来确定匹配项 newPassword: [ null, Validators.compose([ Validators.required, // check whether the entered password has a number CustomValidators.patternValidator(/\d/, {
newPassword: [
null,
Validators.compose([
Validators.required,
// check whether the entered password has a number
CustomValidators.patternValidator(/\d/, {
hasNumber: true
}),
// check whether the entered password has upper case letter
CustomValidators.patternValidator(/[A-Z]/, {
hasCapitalCase: true
}),
// check whether the entered password has a lower case letter
CustomValidators.patternValidator(/[a-z]/, {
hasSmallCase: true
}),
// check whether the entered password has a special character
CustomValidators.patternValidator(
/[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/,
{
hasSpecialCharacters: false
}
),
Validators.minLength(8)
])
]
我的问题是,我如何增强它,例如需要2个大写字母,因为我的函数只检查大写字母是否存在 您需要在正则表达式中添加一个量词。这个量词
{2,}
意味着它必须至少匹配两次
CustomValidators.patternValidator(/[A-Z]{2,}/, {
hasCapitalCase: true
}),
您需要在正则表达式中添加一个量词。这个量词
{2,}
意味着它必须至少匹配两次
CustomValidators.patternValidator(/[A-Z]{2,}/, {
hasCapitalCase: true
}),
Fwiw,强加密码规则是个坏主意。它有助于攻击者,因为它可以向下搜索空间进行暴力攻击。如果您担心密码安全性,则要求最小长度为16。我不担心这一点,因为用户获得初始密码,并且只有在登录后才获得大小写限制。同样在我的情况下,你也需要一个一次性密码通过遥控钥匙登录,这样暴力攻击将有0的机会。同样在5次失败后,源ip被阻止。Fwiw,强制实施密码规则是个坏主意。它有助于攻击者,因为它可以向下搜索空间进行暴力攻击。如果您担心密码安全性,则要求最小长度为16。我不担心这一点,因为用户获得初始密码,并且只有在登录后才获得大小写限制。同样在我的情况下,你也需要一个一次性密码通过遥控钥匙登录,这样暴力攻击将有0的机会。另外,在5失败后,源ip被阻塞。还有一件事,我如何用一个类似于这样的变量来替换2.paswdProfile.upperCase?带nbr的Static可以工作,但当我用变量替换2时,它就不再工作了。CustomValidators.patternValidator(/[A-Z]{this.paswdProfile.upperCase,}/,{还有一件事,我如何用这样的变量替换2.paswdProfile.upperCase?使用nbr进行静态替换?但是当我用变量替换2时,它就不再工作了。CustomValidators.patternValidator(/[A-Z]{this.paswdProfile.upperCase,}/{