如何在primefaces p:password中强制使用强密码

时间:2015-08-27 21:12:25

标签: jsf jsf-2 primefaces passwords

我正在创建一个注册表单。我想强制p:密码很强。有没有办法验证p:密码是否强大?我希望被视为“强”的p:密码反馈的相同指标。

2 个答案:

答案 0 :(得分:2)

我的解决方案:

<h:form id="form" >
    <p:password  value="#{regitroCtrl.usuario.contrasenia}"
                 id="contrasena"
                 feedback="true"   
                 required="true"   
                 onblur="validarPassword()"/>
    <p:message for="contrasena" id="contrasenaMsg" />

    <p:commandButton value="Registrarse"
                     action="#{regitroCtrl.registrarse()}"
                     update="form"
                     onclick="return validarPassword()"/>
</h:form>

validarPassword函数:

function validarPassword(){
    var strength = PrimeFaces.widget.Password.prototype.testStrength($("#form\\:contrasena").val());
    if(strength <= 30){
        $("#form\\:contrasenaMsg").html('<span class="ui-message-error-icon"></span><span class="ui-message-error-detail">Por favor, ingresa una contraseña más fuerte</span>');
        $("#form\\:contrasena").addClass("ui-state-error");
        return false;
    }
    else {
        $("#form\\:contrasenaMsg").html('');
        $("#form\\:contrasena").removeClass("ui-state-error");
        return true;
    }

}

答案 1 :(得分:1)

似乎有no build-in solution符合您的要求。只有我可以在primefaces showcase中看到密码反馈

 <p:password id="pwd" value="#{passwordView.password3}" feedback="true"
      weakLabel="Weak" goodLabel="Good" strongLabel="Strong"/>

或者,您可以使用验证器

   <p:password id="pwd" value="#{passwordView.password3}" feedback="true"
      weakLabel="Weak" goodLabel="Good" strongLabel="Strong">
      <f:validateLength minimum="4" maximum="101"/> // or
      <f:validator  validatorId="passwordValidator" />
   </p:password>

验证类

  @ManagedBean
  @RequestScoped
  @FacesValidator(value = "passwordValidator")
  public class PasswordValidator implements Validator, Serializable {

  @Override
 public void validate(FacesContext fc, UIComponent uic, Object propertyValue) throws ValidatorException {
    // your validation here 
 }