验证PrimeFaces密码的正则表达式

时间:2015-04-01 11:28:59

标签: regex jsf primefaces

我有密码和重复密码字段,但我想验证密码,具体取决于验证正则表达式模式以及匹配密码字段。

           <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>

2 个答案:

答案 0 :(得分:0)

如果我们假设你需要一个正则表达式适合在服务器和客户端运行,我可以建议这个正则表达式

  • 检查密码长度 - .{8,}
  • 确保至少有一位数字 - (?=.*[0-9].*$)
  • 和1个特殊字符(请定义特殊字符对您来说意味着什么,相应地添加或删除) - (?=.*[-+()[\]~!@#$%^&*+}{":;'/?.><, \]。* $)`

正则表达式:

^(?=.*[-+()[\]~!@#$%^&*+}{":;'/?.><,`\\].*$)(?=.*[0-9].*$).{8,}$

答案 1 :(得分:-1)

进行如下更改

<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验证程序以验证正则表达式并比较重复密码和密码

PasswordValidatorBean

@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);

        }

    }
相关问题