2010/09/27

Password quality meter in GWT

A simple password quality meter is part of the gwt-aux project.
It has a very simple API and implementation, but it is very easy to use as well:

// Minimum password length is 6

final int passwordMinimumLength = 6;

// Create password field

final PasswordTextField passwordField = new PasswordTextField();
passwordField.addValidator(new MinLengthValidator(passwordMinimumLength));
addAndReplaceElement(passwordField, "passwordField");

// Create validation result panel

ValidationResultPanel passwordValidationResultPanel = new ValidationResultPanel(passwordField);
addAndReplaceElement(passwordValidationResultPanel, "passwordValidationResultPanel");

// Create password quality meter

final PasswordQualityMeter passwordQualityMeter = new PasswordQualityMeter(passwordMinimumLength);
addAndReplaceElement(passwordQualityMeter, "passwordQualityMeter");

// Validate field and update password quality meter on every key press

passwordField.addKeyUpHandler(new KeyUpHandler()
{
 @Override
 public void onKeyUp(KeyUpEvent event)
 {
  passwordQualityMeter.setPassword(passwordField.getFieldValue());
  passwordField.forceAndVisualizeValidation();
 }
});

Resources:

No comments:

Post a Comment