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

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