验证 PrimeFaces 密码的正则表达式
Validate regex for PrimeFaces Password
我有密码和重复密码字段,但我想根据验证正则表达式模式以及匹配两个密码字段来验证密码。
<p:outputLabel for="Password" value="Password" />
<p:password id="Password" redisplay="true"
value="#{newUserBean.newUserDTO.password}" match="RepeatPassword"
label="Password" required="true"
requiredMessage="Password is required, cannot be empty"
validatorMessage="Password and Repeat Password fields must be same" feedback="true"
promptLabel="Password should contain atleast 8 characters ,1 number and 1 special character" >
</p:password>
<p:outputLabel for="RepeatPassword" value="Repeat Password" />
<p:password id="RepeatPassword" redisplay="true"
value="#{newUserBean.newUserDTO.password}" label="RepeatPassword"
required="true"
requiredMessage="Password is required, cannot be empty" feedback="true"
promptLabel="Repeat Password should match with Password">
</p:password>
如果我们假设您需要一个正则表达式同时适用于服务器端和客户端的 运行,我可以建议这个正则表达式
- 检查密码长度 -
.{8,}
- 确保至少有 1 个数字 -
(?=.*[0-9].*$)
- 和 1 个特殊字符(请定义特殊字符对您的意义,相应地添加或删除)-
(?=.*[-+()[\]~!@#$%^&*+}{":;'/?.><,
\].*$)`
正则表达式:
^(?=.*[-+()[\]~!@#$%^&*+}{":;'/?.><,`\].*$)(?=.*[0-9].*$).{8,}$
进行如下修改
<p:outputLabel for="Password" value="Password" />
<p:password id="Password" redisplay="true"
value="#{newUserBean.newUserDTO.password}"validator="#{passwordValidator.validate}"
label="Password" required="true"
requiredMessage="Password is required, cannot be empty"
validatorMessage="Password and Repeat Password fields must be same" feedback="true"
promptLabel="Password should contain atleast 8 characters ,1 number and 1 special character" >
<p:ajax event="blur" update="msgfrpassword" />
</p:password>
<p:outputLabel for="RepeatPassword" value="Repeat Password" />
<p:password id="RepeatPassword" redisplay="true"
value="#{newUserBean.newUserDTO.password}" label="RepeatPassword" validator="#{confirmPasswordValidator.validate}"
required="true"
requiredMessage="Password is required, cannot be empty" feedback="true"
promptLabel="Repeat Password should match with Password">
<p:ajax event="blur" update="msgfrpassword" />
</p:password>
覆盖 java 验证器以验证正则表达式并比较重复密码和密码
密码验证器Bean
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
matcher = pattern.matcher(value.toString());
if (!matcher.matches()) {
FacesMessage msg = new FacesMessage(
new MessageProvider()
.getValue("prometheus_passwordpromptlable"));
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
我有密码和重复密码字段,但我想根据验证正则表达式模式以及匹配两个密码字段来验证密码。
<p:outputLabel for="Password" value="Password" />
<p:password id="Password" redisplay="true"
value="#{newUserBean.newUserDTO.password}" match="RepeatPassword"
label="Password" required="true"
requiredMessage="Password is required, cannot be empty"
validatorMessage="Password and Repeat Password fields must be same" feedback="true"
promptLabel="Password should contain atleast 8 characters ,1 number and 1 special character" >
</p:password>
<p:outputLabel for="RepeatPassword" value="Repeat Password" />
<p:password id="RepeatPassword" redisplay="true"
value="#{newUserBean.newUserDTO.password}" label="RepeatPassword"
required="true"
requiredMessage="Password is required, cannot be empty" feedback="true"
promptLabel="Repeat Password should match with Password">
</p:password>
如果我们假设您需要一个正则表达式同时适用于服务器端和客户端的 运行,我可以建议这个正则表达式
- 检查密码长度 -
.{8,}
- 确保至少有 1 个数字 -
(?=.*[0-9].*$)
- 和 1 个特殊字符(请定义特殊字符对您的意义,相应地添加或删除)-
(?=.*[-+()[\]~!@#$%^&*+}{":;'/?.><,
\].*$)`
正则表达式:
^(?=.*[-+()[\]~!@#$%^&*+}{":;'/?.><,`\].*$)(?=.*[0-9].*$).{8,}$
进行如下修改
<p:outputLabel for="Password" value="Password" />
<p:password id="Password" redisplay="true"
value="#{newUserBean.newUserDTO.password}"validator="#{passwordValidator.validate}"
label="Password" required="true"
requiredMessage="Password is required, cannot be empty"
validatorMessage="Password and Repeat Password fields must be same" feedback="true"
promptLabel="Password should contain atleast 8 characters ,1 number and 1 special character" >
<p:ajax event="blur" update="msgfrpassword" />
</p:password>
<p:outputLabel for="RepeatPassword" value="Repeat Password" />
<p:password id="RepeatPassword" redisplay="true"
value="#{newUserBean.newUserDTO.password}" label="RepeatPassword" validator="#{confirmPasswordValidator.validate}"
required="true"
requiredMessage="Password is required, cannot be empty" feedback="true"
promptLabel="Repeat Password should match with Password">
<p:ajax event="blur" update="msgfrpassword" />
</p:password>
覆盖 java 验证器以验证正则表达式并比较重复密码和密码
密码验证器Bean
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
matcher = pattern.matcher(value.toString());
if (!matcher.matches()) {
FacesMessage msg = new FacesMessage(
new MessageProvider()
.getValue("prometheus_passwordpromptlable"));
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}