victorpacheco3107
victorpacheco3107

Reputation: 862

How to force strong password in primefaces p:password

I'm creating a registration form. I want to force that p:password is strong. Is there any way to validate that p:password is strong? I want to be taken as 'strong' the same indicator of p:password's feedback.

Upvotes: 3

Views: 5088

Answers (2)

victorpacheco3107
victorpacheco3107

Reputation: 862

My solution for this:

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

The validarPassword function:

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

}

Upvotes: 2

vels4j
vels4j

Reputation: 11298

Seems there is no build-in solution for your requirement. Only I can see it in primefaces showcase is password feed back

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

Alternately you can use validator

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

Validation class

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

Upvotes: 1

Related Questions