Posted By

aviddv1 on 05/22/10


Tagged

regex javascript password jquery strength meter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wattsupnow


Password Strength Meter


 / Published in: jQuery
 

  1. PASSWORD STRENGTH METER (jquery)
  2. /**
  3. * Evaluate the strength of a user's password.
  4. *
  5. * Returns the estimated strength and the relevant output message.
  6. */
  7. Drupal.evaluatePasswordStrength = function(value) {
  8. var strength = "", msg = "", translate = Drupal.settings.password;
  9. var hasLetters = value.match(/[a-zA-Z]+/);
  10. var hasNumbers = value.match(/[0-9]+/);
  11. var hasPunctuation = value.match(/[^a-zA-Z0-9]+/);
  12. var hasCasing = value.match(/[a-z]+.*[A-Z]+|[A-Z]+.*[a-z]+/);
  13. // Check if the password is blank.
  14. if (!value.length) {
  15. strength = "";
  16. msg = "";
  17. }
  18. // Check if length is less than 6 characters.
  19. else if (value.length < 6) {
  20. strength = "low";
  21. msg = translate.tooShort;
  22. }
  23. // Check if password is the same as the username (convert both to lowercase).
  24. else if (value.toLowerCase() == translate.username.toLowerCase()) {
  25. strength = "low";
  26. msg = translate.sameAsUsername;
  27. }
  28. // Check if it contains letters, numbers, punctuation, and upper/lower case.
  29. else if (hasLetters && hasNumbers && hasPunctuation && hasCasing) {
  30. strength = "high";
  31. }
  32. // Password is not secure enough so construct the medium-strength message.
  33. else {
  34. // Extremely bad passwords still count as low.
  35. var count = (hasLetters ? 1 : 0) + (hasNumbers ? 1 : 0) + (hasPunctuation ? 1 : 0) + (hasCasing ? 1 : 0);
  36. strength = count > 1 ? "medium" : "low";
  37. msg = [];
  38. if (!hasLetters || !hasCasing) {
  39. msg.push(translate.addLetters);
  40. }
  41. if (!hasNumbers) {
  42. msg.push(translate.addNumbers);
  43. }
  44. if (!hasPunctuation) {
  45. msg.push(translate.addPunctuation);
  46. }
  47. msg = translate.needsMoreVariation +"<ul><li>"+ msg.join("</li><li>") +"</li></ul>";
  48. }
  49. return { strength: strength, message: msg };
  50. };

Report this snippet  

You need to login to post a comment.