BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

password_validate.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Validates passwords to make sure they are powerful
  2. (function () {
  3. var CLEAR = 0;
  4. var WEAK = 1;
  5. var STRONG = 3;
  6. var SHORT = 4;
  7. var MATCH_IRCKEY = 5;
  8. var MATCH_USERNAME = 6;
  9. var COMMON = 7;
  10. var USER_PATH = "/user.php";
  11. $(document).ready(function () {
  12. var old = $("#new_pass_1").val().length;
  13. var password1;
  14. var password2;
  15. $("#new_pass_1").keyup(function () {
  16. password1 = $("#new_pass_1").val();
  17. if (password1.length != old) {
  18. disableSubmit();
  19. calculateComplexity(password1);
  20. old = password1.length;
  21. }
  22. });
  23. $("#new_pass_1").change(function () {
  24. password1 = $("#new_pass_1").val();
  25. password2 = $("#new_pass_2").val();
  26. if (password1.length == 0 && password2.length == 0) {
  27. enableSubmit();
  28. }
  29. });
  30. $("#new_pass_1").focus(function () {
  31. password1 = $("#new_pass_1").val();
  32. password2 = $("#new_pass_2").val();
  33. if (password1.length > 0) {
  34. checkMatching(password1, password2);
  35. }
  36. });
  37. $("#new_pass_2").keyup(function () {
  38. password2 = $("#new_pass_2").val();
  39. checkMatching(password1, password2);
  40. });
  41. $("#new_pass_1").blur(function () {
  42. password1 = $("#new_pass_1").val();
  43. password2 = $("#new_pass_2").val();
  44. if (password1.length == 0 && password2.length == 0) {
  45. enableSubmit();
  46. }
  47. });
  48. });
  49. function calculateComplexity(password) {
  50. var length = password.length;
  51. var username;
  52. if (isUserPage()) {
  53. username = $(".username").text();
  54. } else {
  55. username = $("#username").val() || '';
  56. }
  57. var irckey;
  58. if (isUserPage()) {
  59. irckey = $("#irckey").val();
  60. }
  61. if (length >= 15) {
  62. setStatus(WEAK);
  63. }
  64. if (length >= 15 && isStrongPassword(password)) {
  65. setStatus(STRONG);
  66. }
  67. if (length > 0 && length < 15) {
  68. setStatus(SHORT);
  69. }
  70. if (length == 0) {
  71. setStatus(CLEAR);
  72. }
  73. if (isUserPage()) {
  74. if (irckey.length > 0) {
  75. if (password.toLowerCase() == irckey.toLowerCase()) {
  76. setStatus(MATCH_IRCKEY);
  77. }
  78. }
  79. }
  80. if (username.length > 0) {
  81. if (password.toLowerCase() == username.toLowerCase()) {
  82. setStatus(MATCH_USERNAME);
  83. }
  84. }
  85. }
  86. function isStrongPassword(password) {
  87. return /(?=^.{6,}$).*$/.test(password);
  88. }
  89. function checkMatching(password1, password2) {
  90. if (password2.length > 0) {
  91. if (password1 == password2 && getStrong() == true) {
  92. $("#pass_match").text("Passwords match").css("color", "green");
  93. enableSubmit();
  94. } else if (getStrong() == true) {
  95. $("#pass_match").text("Passwords do not match").css("color", "red");
  96. disableSubmit();
  97. } else {
  98. $("#pass_match").text("Password isn't strong").css("color", "red");
  99. disableSubmit();
  100. }
  101. } else {
  102. $("#pass_match").text("");
  103. }
  104. }
  105. function getStrong() {
  106. return $("#pass_strength").text() == "Strong";
  107. }
  108. function setStatus(strength) {
  109. if (strength == WEAK) {
  110. disableSubmit();
  111. $("#pass_strength").text("Weak").css("color", "red");
  112. }
  113. if (strength == STRONG) {
  114. disableSubmit();
  115. $("#pass_strength").text("Strong").css("color", "green");
  116. }
  117. if (strength == SHORT) {
  118. disableSubmit();
  119. $("#pass_strength").text("Too Short").css("color", "red");
  120. }
  121. if (strength == MATCH_IRCKEY) {
  122. disableSubmit();
  123. $("#pass_strength").text("Password cannot match IRC Key").css("color", "red");
  124. }
  125. if (strength == MATCH_USERNAME) {
  126. disableSubmit();
  127. $("#pass_strength").text("Password cannot match Username").css("color", "red");
  128. }
  129. if (strength == COMMON) {
  130. disableSubmit();
  131. $("#pass_strength").text("Password is too common").css("color", "red");
  132. }
  133. if (strength == CLEAR) {
  134. $("#pass_strength").text("");
  135. }
  136. }
  137. function disableSubmit() {
  138. $('input[type="submit"]').attr('disabled', 'disabled');
  139. }
  140. function enableSubmit() {
  141. $('input[type="submit"]').removeAttr('disabled');
  142. }
  143. function isUserPage() {
  144. return window.location.pathname.indexOf(USER_PATH) != -1;
  145. }
  146. })();