Oppaitime'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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. } else if (getStrong() == true) {
  29. validatePassword(password1);
  30. }
  31. });
  32. $("#new_pass_1").focus(function() {
  33. password1 = $("#new_pass_1").val();
  34. password2 = $("#new_pass_2").val();
  35. if (password1.length > 0) {
  36. checkMatching(password1, password2);
  37. }
  38. });
  39. $("#new_pass_2").keyup(function() {
  40. password2 = $("#new_pass_2").val();
  41. checkMatching(password1, password2);
  42. });
  43. $("#new_pass_1").blur(function() {
  44. password1 = $("#new_pass_1").val();
  45. password2 = $("#new_pass_2").val();
  46. if (password1.length == 0 && password2.length == 0) {
  47. enableSubmit();
  48. }
  49. });
  50. });
  51. function validatePassword(password) {
  52. if (isUserPage()) {
  53. $.ajax({
  54. type: 'POST',
  55. dataType: 'text',
  56. url : 'ajax.php?action=password_validate',
  57. data: 'password=' + password,
  58. async: false,
  59. success: function(value) {
  60. if (value == 'false') {
  61. setStatus(COMMON);
  62. }
  63. }
  64. });
  65. }
  66. }
  67. function calculateComplexity(password) {
  68. var length = password.length;
  69. var username;
  70. if (isUserPage()) {
  71. username = $(".username").text();
  72. }
  73. else {
  74. username = $("#username").val() || '';
  75. }
  76. var irckey;
  77. if (isUserPage()) {
  78. irckey = $("#irckey").val();
  79. }
  80. if (length >= 6) {
  81. setStatus(WEAK);
  82. }
  83. if (length >= 6 && isStrongPassword(password)) {
  84. setStatus(STRONG);
  85. }
  86. if (length > 0 && length < 6) {
  87. setStatus(SHORT);
  88. }
  89. if (length == 0) {
  90. setStatus(CLEAR);
  91. }
  92. if (isUserPage()) {
  93. if (irckey.length > 0) {
  94. if (password.toLowerCase() == irckey.toLowerCase()) {
  95. setStatus(MATCH_IRCKEY);
  96. }
  97. }
  98. }
  99. if (username.length > 0) {
  100. if (password.toLowerCase() == username.toLowerCase()) {
  101. setStatus(MATCH_USERNAME);
  102. }
  103. }
  104. }
  105. function isStrongPassword(password) {
  106. return /(?=^.{6,}$).*$/.test(password);
  107. }
  108. function checkMatching(password1, password2) {
  109. if (password2.length > 0) {
  110. if (password1 == password2 && getStrong() == true) {
  111. $("#pass_match").text("Passwords match").css("color", "green");
  112. enableSubmit();
  113. } else if (getStrong() == true) {
  114. $("#pass_match").text("Passwords do not match").css("color", "red");
  115. disableSubmit();
  116. } else {
  117. $("#pass_match").text("Password isn't strong").css("color", "red");
  118. disableSubmit();
  119. }
  120. } else {
  121. $("#pass_match").text("");
  122. }
  123. }
  124. function getStrong() {
  125. return $("#pass_strength").text() == "Strong";
  126. }
  127. function setStatus(strength) {
  128. if (strength == WEAK) {
  129. disableSubmit();
  130. $("#pass_strength").text("Weak").css("color", "red");
  131. }
  132. if (strength == STRONG) {
  133. disableSubmit();
  134. $("#pass_strength").text("Strong").css("color", "green");
  135. }
  136. if (strength == SHORT) {
  137. disableSubmit();
  138. $("#pass_strength").text("Too Short").css("color", "red");
  139. }
  140. if (strength == MATCH_IRCKEY) {
  141. disableSubmit();
  142. $("#pass_strength").text("Password cannot match IRC Key").css("color", "red");
  143. }
  144. if (strength == MATCH_USERNAME) {
  145. disableSubmit();
  146. $("#pass_strength").text("Password cannot match Username").css("color", "red");
  147. }
  148. if (strength == COMMON) {
  149. disableSubmit();
  150. $("#pass_strength").text("Password is too common").css("color", "red");
  151. }
  152. if (strength == CLEAR) {
  153. $("#pass_strength").text("");
  154. }
  155. }
  156. function disableSubmit() {
  157. $('input[type="submit"]').attr('disabled','disabled');
  158. }
  159. function enableSubmit() {
  160. $('input[type="submit"]').removeAttr('disabled');
  161. }
  162. function isUserPage() {
  163. return window.location.pathname.indexOf(USER_PATH) != -1;
  164. }
  165. })();