jmorhardt
jmorhardt

Reputation: 183

jQuery password strength plugin callback validation method

I'm using a a jQuery plugin to evaluate password strength. It gives a graphical representation for the user to see how secure the password is. I'd like to use it to validate the field as well.

The plugin works by assessing the password and giving it a score. I want to be able to verify that the user has entered a password of at least a certain score. The code is hosted on jQuery's site here: http://plugins.jquery.com/project/pstrength.

The documentation states that there is a way to add a rule and do custom validation. I'm not sure where to start. The inline documentation states:

 * === Changelog ===
 * Version 2.1 (18/05/2008)
 * Added a jQuery method to add a new rule: jQuery('input[@type=password]').pstrength.addRule(name, method, score, active)

And later in the code there's this method:

jQuery.extend(jQuery.fn.pstrength, {
    'addRule': function (name, method, score, active) {
        digitalspaghetti.password.addRule(name, method, score, active);
        return true;
    },
    'changeScore': function (rule, score) {
        digitalspaghetti.password.ruleScores[rule] = score;
        return true;
    },
    'ruleActive': function (rule, active) {
        digitalspaghetti.password.rules[rule] = active;
        return true;
    }
});

If anybody has seen an example of how to do this I'd appreciate a pointer in the right direction. Thanks!

Upvotes: 1

Views: 1636

Answers (1)

sbmaxx
sbmaxx

Reputation: 896

You can you this code.


if (digitalspaghetti.password.totalscore >= 43) {
  console.log("it's ok");
}

Where:

  • <= 20 == week
  • <= 30 == normal
  • <= 43 == medium
  • <= 50 == strong
  • 50 == very strong

Instant validation:


$("#password").keypress(function () {
  if (digitalspaghetti.password.totalscore >= 43) {
    console.log("it's ok");
  }
});

Before form submit;


$("#password").parents("form").submit(function () {
  if (digitalspaghetti.password.totalscore >= 43) {
    console.log("it's ok");
  } else {
    return false;
  } 
});

Upvotes: 1

Related Questions