密码强度计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了密码强度计相关的知识,希望对你有一定的参考价值。

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

以上是关于密码强度计的主要内容,如果未能解决你的问题,请参考以下文章

推特引导程序的jQuery密码强度计

ActionScript 3 AS3密码强度计

密码强度计

密码强度算法的标准化

如何在 Ruby on Rails 中使用 Devise 验证密码强度?

在密码强度检查器中使用一组不可用的单词