pwstrength-bootstrap.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*!
  2. * jQuery Password Strength plugin for Twitter Bootstrap
  3. * Version: 2.0.8
  4. *
  5. * Copyright (c) 2008-2013 Tane Piper
  6. * Copyright (c) 2013 Alejandro Blanco
  7. * Dual licensed under the MIT and GPL licenses.
  8. */
  9. (function (jQuery) {
  10. // Source: src/i18n.js
  11. var i18n = {};
  12. (function (i18n, i18next) {
  13. 'use strict';
  14. i18n.fallback = {
  15. "wordLength": "Your password is too short",
  16. "wordNotEmail": "Do not use your email as your password",
  17. "wordSimilarToUsername": "Your password cannot contain your username",
  18. "wordTwoCharacterClasses": "Use different character classes",
  19. "wordRepetitions": "Too many repetitions",
  20. "wordSequences": "Your password contains sequences",
  21. "errorList": "Errors:",
  22. "veryWeak": "Very Weak",
  23. "weak": "Weak",
  24. "normal": "Normal",
  25. "medium": "Medium",
  26. "strong": "Strong",
  27. "veryStrong": "Very Strong"
  28. };
  29. i18n.t = function (key) {
  30. var result = '';
  31. // Try to use i18next.com
  32. if (i18next) {
  33. result = i18next.t(key);
  34. } else {
  35. // Fallback to english
  36. result = i18n.fallback[key];
  37. }
  38. return result === key ? '' : result;
  39. };
  40. }(i18n, window.i18next));
  41. // Source: src/rules.js
  42. var rulesEngine = {};
  43. try {
  44. if (!jQuery && module && module.exports) {
  45. var jQuery = require("jquery"),
  46. jsdom = require("jsdom").jsdom;
  47. jQuery = jQuery(jsdom().defaultView);
  48. }
  49. } catch (ignore) {}
  50. (function ($, rulesEngine) {
  51. "use strict";
  52. var validation = {};
  53. rulesEngine.forbiddenSequences = [
  54. "0123456789", "abcdefghijklmnopqrstuvwxyz", "qwertyuiop", "asdfghjkl",
  55. "zxcvbnm", "!@#$%^&*()_+"
  56. ];
  57. validation.wordNotEmail = function (options, word, score) {
  58. if (word.match(/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i)) {
  59. return score;
  60. }
  61. return 0;
  62. };
  63. validation.wordLength = function (options, word, score) {
  64. var wordlen = word.length,
  65. lenScore = Math.pow(wordlen, options.rules.raisePower);
  66. if (wordlen < options.common.minChar) {
  67. lenScore = (lenScore + score);
  68. }
  69. return lenScore;
  70. };
  71. validation.wordSimilarToUsername = function (options, word, score) {
  72. var username = $(options.common.usernameField).val();
  73. if (username && word.toLowerCase().match(username.replace(/[\-\[\]\/\{\}\(\)\*\+\=\?\:\.\\\^\$\|\!\,]/g, "\\$&").toLowerCase())) {
  74. return score;
  75. }
  76. return 0;
  77. };
  78. validation.wordTwoCharacterClasses = function (options, word, score) {
  79. if (word.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) ||
  80. (word.match(/([a-zA-Z])/) && word.match(/([0-9])/)) ||
  81. (word.match(/(.[!,@,#,$,%,\^,&,*,?,_,~])/) && word.match(/[a-zA-Z0-9_]/))) {
  82. return score;
  83. }
  84. return 0;
  85. };
  86. validation.wordRepetitions = function (options, word, score) {
  87. if (word.match(/(.)\1\1/)) { return score; }
  88. return 0;
  89. };
  90. validation.wordSequences = function (options, word, score) {
  91. var found = false,
  92. j;
  93. if (word.length > 2) {
  94. $.each(rulesEngine.forbiddenSequences, function (idx, seq) {
  95. if (found) { return; }
  96. var sequences = [seq, seq.split('').reverse().join('')];
  97. $.each(sequences, function (idx, sequence) {
  98. for (j = 0; j < (word.length - 2); j += 1) { // iterate the word trough a sliding window of size 3:
  99. if (sequence.indexOf(word.toLowerCase().substring(j, j + 3)) > -1) {
  100. found = true;
  101. }
  102. }
  103. });
  104. });
  105. if (found) { return score; }
  106. }
  107. return 0;
  108. };
  109. validation.wordLowercase = function (options, word, score) {
  110. return word.match(/[a-z]/) && score;
  111. };
  112. validation.wordUppercase = function (options, word, score) {
  113. return word.match(/[A-Z]/) && score;
  114. };
  115. validation.wordOneNumber = function (options, word, score) {
  116. return word.match(/\d+/) && score;
  117. };
  118. validation.wordThreeNumbers = function (options, word, score) {
  119. return word.match(/(.*[0-9].*[0-9].*[0-9])/) && score;
  120. };
  121. validation.wordOneSpecialChar = function (options, word, score) {
  122. return word.match(/[!,@,#,$,%,\^,&,*,?,_,~]/) && score;
  123. };
  124. validation.wordTwoSpecialChar = function (options, word, score) {
  125. return word.match(/(.*[!,@,#,$,%,\^,&,*,?,_,~].*[!,@,#,$,%,\^,&,*,?,_,~])/) && score;
  126. };
  127. validation.wordUpperLowerCombo = function (options, word, score) {
  128. return word.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) && score;
  129. };
  130. validation.wordLetterNumberCombo = function (options, word, score) {
  131. return word.match(/([a-zA-Z])/) && word.match(/([0-9])/) && score;
  132. };
  133. validation.wordLetterNumberCharCombo = function (options, word, score) {
  134. return word.match(/([a-zA-Z0-9].*[!,@,#,$,%,\^,&,*,?,_,~])|([!,@,#,$,%,\^,&,*,?,_,~].*[a-zA-Z0-9])/) && score;
  135. };
  136. rulesEngine.validation = validation;
  137. rulesEngine.executeRules = function (options, word) {
  138. var totalScore = 0;
  139. $.each(options.rules.activated, function (rule, active) {
  140. if (active) {
  141. var score = options.rules.scores[rule],
  142. funct = rulesEngine.validation[rule],
  143. result,
  144. errorMessage;
  145. if (!$.isFunction(funct)) {
  146. funct = options.rules.extra[rule];
  147. }
  148. if ($.isFunction(funct)) {
  149. result = funct(options, word, score);
  150. if (result) {
  151. totalScore += result;
  152. }
  153. if (result < 0 || (!$.isNumeric(result) && !result)) {
  154. errorMessage = options.ui.spanError(options, rule);
  155. if (errorMessage.length > 0) {
  156. options.instances.errors.push(errorMessage);
  157. }
  158. }
  159. }
  160. }
  161. });
  162. if ($.isFunction(options.common.onScore)) {
  163. totalScore = options.common.onScore(options, word, totalScore);
  164. }
  165. return totalScore;
  166. };
  167. }(jQuery, rulesEngine));
  168. try {
  169. if (module && module.exports) {
  170. module.exports = rulesEngine;
  171. }
  172. } catch (ignore) {}
  173. // Source: src/options.js
  174. var defaultOptions = {};
  175. defaultOptions.common = {};
  176. defaultOptions.common.minChar = 6;
  177. defaultOptions.common.usernameField = "#username";
  178. defaultOptions.common.userInputs = [
  179. // Selectors for input fields with user input
  180. ];
  181. defaultOptions.common.onLoad = undefined;
  182. defaultOptions.common.onKeyUp = undefined;
  183. defaultOptions.common.onScore = undefined;
  184. defaultOptions.common.zxcvbn = false;
  185. defaultOptions.common.zxcvbnTerms = [
  186. // List of disrecommended words
  187. ];
  188. defaultOptions.common.events = ["keyup", "change", "paste"];
  189. defaultOptions.common.debug = false;
  190. defaultOptions.rules = {};
  191. defaultOptions.rules.extra = {};
  192. defaultOptions.rules.scores = {
  193. wordNotEmail: -100,
  194. wordLength: -50,
  195. wordSimilarToUsername: -100,
  196. wordSequences: -20,
  197. wordTwoCharacterClasses: 2,
  198. wordRepetitions: -25,
  199. wordLowercase: 1,
  200. wordUppercase: 3,
  201. wordOneNumber: 3,
  202. wordThreeNumbers: 5,
  203. wordOneSpecialChar: 3,
  204. wordTwoSpecialChar: 5,
  205. wordUpperLowerCombo: 2,
  206. wordLetterNumberCombo: 2,
  207. wordLetterNumberCharCombo: 2
  208. };
  209. defaultOptions.rules.activated = {
  210. wordNotEmail: true,
  211. wordLength: true,
  212. wordSimilarToUsername: true,
  213. wordSequences: true,
  214. wordTwoCharacterClasses: false,
  215. wordRepetitions: false,
  216. wordLowercase: true,
  217. wordUppercase: true,
  218. wordOneNumber: true,
  219. wordThreeNumbers: true,
  220. wordOneSpecialChar: true,
  221. wordTwoSpecialChar: true,
  222. wordUpperLowerCombo: true,
  223. wordLetterNumberCombo: true,
  224. wordLetterNumberCharCombo: true
  225. };
  226. defaultOptions.rules.raisePower = 1.4;
  227. defaultOptions.ui = {};
  228. defaultOptions.ui.bootstrap2 = false;
  229. defaultOptions.ui.bootstrap4 = false;
  230. defaultOptions.ui.colorClasses = [
  231. "danger", "danger", "danger", "warning", "warning", "success"
  232. ];
  233. defaultOptions.ui.showProgressBar = true;
  234. defaultOptions.ui.progressBarEmptyPercentage = 1;
  235. defaultOptions.ui.progressBarMinPercentage = 1;
  236. defaultOptions.ui.progressExtraCssClasses = '';
  237. defaultOptions.ui.progressBarExtraCssClasses = '';
  238. defaultOptions.ui.showPopover = false;
  239. defaultOptions.ui.popoverPlacement = "bottom";
  240. defaultOptions.ui.showStatus = false;
  241. defaultOptions.ui.spanError = function (options, key) {
  242. "use strict";
  243. var text = options.i18n.t(key);
  244. if (!text) { return ''; }
  245. return '<span style="color: #d52929">' + text + '</span>';
  246. };
  247. defaultOptions.ui.popoverError = function (options) {
  248. "use strict";
  249. var errors = options.instances.errors,
  250. errorsTitle = options.i18n.t("errorList"),
  251. message = "<div>" + errorsTitle + "<ul class='error-list' style='margin-bottom: 0;'>";
  252. jQuery.each(errors, function (idx, err) {
  253. message += "<li>" + err + "</li>";
  254. });
  255. message += "</ul></div>";
  256. return message;
  257. };
  258. defaultOptions.ui.showVerdicts = true;
  259. defaultOptions.ui.showVerdictsInsideProgressBar = false;
  260. defaultOptions.ui.useVerdictCssClass = false;
  261. defaultOptions.ui.showErrors = false;
  262. defaultOptions.ui.showScore = false;
  263. defaultOptions.ui.container = undefined;
  264. defaultOptions.ui.viewports = {
  265. progress: undefined,
  266. verdict: undefined,
  267. errors: undefined,
  268. score: undefined
  269. };
  270. defaultOptions.ui.scores = [0, 14, 26, 38, 50];
  271. defaultOptions.i18n = {};
  272. defaultOptions.i18n.t = i18n.t;
  273. // Source: src/ui.js
  274. var ui = {};
  275. (function ($, ui) {
  276. "use strict";
  277. var statusClasses = ["error", "warning", "success"],
  278. verdictKeys = [
  279. "veryWeak", "weak", "normal", "medium", "strong", "veryStrong"
  280. ];
  281. ui.getContainer = function (options, $el) {
  282. var $container;
  283. $container = $(options.ui.container);
  284. if (!($container && $container.length === 1)) {
  285. $container = $el.parent();
  286. }
  287. return $container;
  288. };
  289. ui.findElement = function ($container, viewport, cssSelector) {
  290. if (viewport) {
  291. return $container.find(viewport).find(cssSelector);
  292. }
  293. return $container.find(cssSelector);
  294. };
  295. ui.getUIElements = function (options, $el) {
  296. var $container, result;
  297. if (options.instances.viewports) {
  298. return options.instances.viewports;
  299. }
  300. $container = ui.getContainer(options, $el);
  301. result = {};
  302. result.$progressbar = ui.findElement($container, options.ui.viewports.progress, "div.progress");
  303. if (options.ui.showVerdictsInsideProgressBar) {
  304. result.$verdict = result.$progressbar.find("span.password-verdict");
  305. }
  306. if (!options.ui.showPopover) {
  307. if (!options.ui.showVerdictsInsideProgressBar) {
  308. result.$verdict = ui.findElement($container, options.ui.viewports.verdict, "span.password-verdict");
  309. }
  310. result.$errors = ui.findElement($container, options.ui.viewports.errors, "ul.error-list");
  311. }
  312. result.$score = ui.findElement($container, options.ui.viewports.score,
  313. "span.password-score");
  314. options.instances.viewports = result;
  315. return result;
  316. };
  317. ui.initProgressBar = function (options, $el) {
  318. var $container = ui.getContainer(options, $el),
  319. progressbar = "<div class='progress ";
  320. if (options.ui.bootstrap2) {
  321. // Boostrap 2
  322. progressbar += options.ui.progressBarExtraCssClasses +
  323. "'><div class='";
  324. } else {
  325. // Bootstrap 3 & 4
  326. progressbar += options.ui.progressExtraCssClasses + "'><div class='" +
  327. options.ui.progressBarExtraCssClasses + " progress-";
  328. }
  329. progressbar += "bar'>";
  330. if (options.ui.showVerdictsInsideProgressBar) {
  331. progressbar += "<span class='password-verdict'></span>";
  332. }
  333. progressbar += "</div></div>";
  334. if (options.ui.viewports.progress) {
  335. $container.find(options.ui.viewports.progress).append(progressbar);
  336. } else {
  337. $(progressbar).insertAfter($el);
  338. }
  339. };
  340. ui.initHelper = function (options, $el, html, viewport) {
  341. var $container = ui.getContainer(options, $el);
  342. if (viewport) {
  343. $container.find(viewport).append(html);
  344. } else {
  345. $(html).insertAfter($el);
  346. }
  347. };
  348. ui.initVerdict = function (options, $el) {
  349. ui.initHelper(options, $el, "<span class='password-verdict'></span>",
  350. options.ui.viewports.verdict);
  351. };
  352. ui.initErrorList = function (options, $el) {
  353. ui.initHelper(options, $el, "<ul class='error-list'></ul>",
  354. options.ui.viewports.errors);
  355. };
  356. ui.initScore = function (options, $el) {
  357. ui.initHelper(options, $el, "<span class='password-score'></span>",
  358. options.ui.viewports.score);
  359. };
  360. ui.initPopover = function (options, $el) {
  361. $el.popover("destroy");
  362. $el.popover({
  363. html: true,
  364. placement: options.ui.popoverPlacement,
  365. trigger: "manual",
  366. content: " "
  367. });
  368. };
  369. ui.initUI = function (options, $el) {
  370. if (options.ui.showPopover) {
  371. ui.initPopover(options, $el);
  372. } else {
  373. if (options.ui.showErrors) { ui.initErrorList(options, $el); }
  374. if (options.ui.showVerdicts && !options.ui.showVerdictsInsideProgressBar) {
  375. ui.initVerdict(options, $el);
  376. }
  377. }
  378. if (options.ui.showProgressBar) {
  379. ui.initProgressBar(options, $el);
  380. }
  381. if (options.ui.showScore) {
  382. ui.initScore(options, $el);
  383. }
  384. };
  385. ui.updateProgressBar = function (options, $el, cssClass, percentage) {
  386. var $progressbar = ui.getUIElements(options, $el).$progressbar,
  387. $bar = $progressbar.find(".progress-bar"),
  388. cssPrefix = "progress-";
  389. if (options.ui.bootstrap2) {
  390. $bar = $progressbar.find(".bar");
  391. cssPrefix = "";
  392. }
  393. $.each(options.ui.colorClasses, function (idx, value) {
  394. if (options.ui.bootstrap4) {
  395. $bar.removeClass("bg-" + value);
  396. } else {
  397. $bar.removeClass(cssPrefix + "bar-" + value);
  398. }
  399. });
  400. if (options.ui.bootstrap4) {
  401. $bar.addClass("bg-" + options.ui.colorClasses[cssClass]);
  402. } else {
  403. $bar.addClass(cssPrefix + "bar-" + options.ui.colorClasses[cssClass]);
  404. }
  405. $bar.css("width", percentage + '%');
  406. };
  407. ui.updateVerdict = function (options, $el, cssClass, text) {
  408. var $verdict = ui.getUIElements(options, $el).$verdict;
  409. $verdict.removeClass(options.ui.colorClasses.join(' '));
  410. if (cssClass > -1) {
  411. $verdict.addClass(options.ui.colorClasses[cssClass]);
  412. }
  413. $verdict.html(text);
  414. };
  415. ui.updateErrors = function (options, $el, remove) {
  416. var $errors = ui.getUIElements(options, $el).$errors,
  417. html = "";
  418. if (!remove) {
  419. $.each(options.instances.errors, function (idx, err) {
  420. html += "<li>" + err + "</li>";
  421. });
  422. }
  423. $errors.html(html);
  424. };
  425. ui.updateScore = function (options, $el, score, remove) {
  426. var $score = ui.getUIElements(options, $el).$score,
  427. html = "";
  428. if (!remove) { html = score.toFixed(2); }
  429. $score.html(html);
  430. };
  431. ui.updatePopover = function (options, $el, verdictText, remove) {
  432. var popover = $el.data("bs.popover"),
  433. html = "",
  434. hide = true;
  435. if (options.ui.showVerdicts &&
  436. !options.ui.showVerdictsInsideProgressBar &&
  437. verdictText.length > 0) {
  438. html = "<h5><span class='password-verdict'>" + verdictText +
  439. "</span></h5>";
  440. hide = false;
  441. }
  442. if (options.ui.showErrors) {
  443. if (options.instances.errors.length > 0) {
  444. hide = false;
  445. }
  446. html += options.ui.popoverError(options);
  447. }
  448. if (hide || remove) {
  449. $el.popover("hide");
  450. return;
  451. }
  452. if (options.ui.bootstrap2) { popover = $el.data("popover"); }
  453. if (popover.$arrow && popover.$arrow.parents("body").length > 0) {
  454. $el.find("+ .popover .popover-content").html(html);
  455. } else {
  456. // It's hidden
  457. popover.options.content = html;
  458. $el.popover("show");
  459. }
  460. };
  461. ui.updateFieldStatus = function (options, $el, cssClass, remove) {
  462. var targetClass = options.ui.bootstrap2 ? ".control-group" : ".form-group",
  463. $container = $el.parents(targetClass).first();
  464. $.each(statusClasses, function (idx, css) {
  465. if (!options.ui.bootstrap2) { css = "has-" + css; }
  466. $container.removeClass(css);
  467. });
  468. if (remove) { return; }
  469. cssClass = statusClasses[Math.floor(cssClass / 2)];
  470. if (!options.ui.bootstrap2) { cssClass = "has-" + cssClass; }
  471. $container.addClass(cssClass);
  472. };
  473. ui.percentage = function (options, score, maximun) {
  474. var result = Math.floor(100 * score / maximun),
  475. min = options.ui.progressBarMinPercentage;
  476. result = result <= min ? min : result;
  477. result = result > 100 ? 100 : result;
  478. return result;
  479. };
  480. ui.getVerdictAndCssClass = function (options, score) {
  481. var level, verdict;
  482. if (score === undefined) { return ['', 0]; }
  483. if (score <= options.ui.scores[0]) {
  484. level = 0;
  485. } else if (score < options.ui.scores[1]) {
  486. level = 1;
  487. } else if (score < options.ui.scores[2]) {
  488. level = 2;
  489. } else if (score < options.ui.scores[3]) {
  490. level = 3;
  491. } else if (score < options.ui.scores[4]) {
  492. level = 4;
  493. } else {
  494. level = 5;
  495. }
  496. verdict = verdictKeys[level];
  497. return [options.i18n.t(verdict), level];
  498. };
  499. ui.updateUI = function (options, $el, score) {
  500. var cssClass, barPercentage, verdictText, verdictCssClass;
  501. cssClass = ui.getVerdictAndCssClass(options, score);
  502. verdictText = score === 0 ? '' : cssClass[0];
  503. cssClass = cssClass[1];
  504. verdictCssClass = options.ui.useVerdictCssClass ? cssClass : -1;
  505. if (options.ui.showProgressBar) {
  506. if (score === undefined) {
  507. barPercentage = options.ui.progressBarEmptyPercentage;
  508. } else {
  509. barPercentage = ui.percentage(options, score, options.ui.scores[4]);
  510. }
  511. ui.updateProgressBar(options, $el, cssClass, barPercentage);
  512. if (options.ui.showVerdictsInsideProgressBar) {
  513. ui.updateVerdict(options, $el, verdictCssClass, verdictText);
  514. }
  515. }
  516. if (options.ui.showStatus) {
  517. ui.updateFieldStatus(options, $el, cssClass, score === undefined);
  518. }
  519. if (options.ui.showPopover) {
  520. ui.updatePopover(options, $el, verdictText, score === undefined);
  521. } else {
  522. if (options.ui.showVerdicts && !options.ui.showVerdictsInsideProgressBar) {
  523. ui.updateVerdict(options, $el, verdictCssClass, verdictText);
  524. }
  525. if (options.ui.showErrors) {
  526. ui.updateErrors(options, $el, score === undefined);
  527. }
  528. }
  529. if (options.ui.showScore) {
  530. ui.updateScore(options, $el, score, score === undefined);
  531. }
  532. };
  533. }(jQuery, ui));
  534. // Source: src/methods.js
  535. var methods = {};
  536. (function ($, methods) {
  537. "use strict";
  538. var onKeyUp, onPaste, applyToAll;
  539. onKeyUp = function (event) {
  540. var $el = $(event.target),
  541. options = $el.data("pwstrength-bootstrap"),
  542. word = $el.val(),
  543. userInputs,
  544. verdictText,
  545. verdictLevel,
  546. score;
  547. if (options === undefined) { return; }
  548. options.instances.errors = [];
  549. if (word.length === 0) {
  550. score = undefined;
  551. } else {
  552. if (options.common.zxcvbn) {
  553. userInputs = [];
  554. $.each(options.common.userInputs.concat([options.common.usernameField]), function (idx, selector) {
  555. var value = $(selector).val();
  556. if (value) { userInputs.push(value); }
  557. });
  558. userInputs = userInputs.concat(options.common.zxcvbnTerms);
  559. score = zxcvbn(word, userInputs).guesses;
  560. score = Math.log(score) * Math.LOG2E;
  561. } else {
  562. score = rulesEngine.executeRules(options, word);
  563. }
  564. }
  565. ui.updateUI(options, $el, score);
  566. verdictText = ui.getVerdictAndCssClass(options, score);
  567. verdictLevel = verdictText[1];
  568. verdictText = verdictText[0];
  569. if (options.common.debug) { console.log(score + ' - ' + verdictText); }
  570. if ($.isFunction(options.common.onKeyUp)) {
  571. options.common.onKeyUp(event, {
  572. score: score,
  573. verdictText: verdictText,
  574. verdictLevel: verdictLevel
  575. });
  576. }
  577. };
  578. onPaste = function (event) {
  579. // This handler is necessary because the paste event fires before the
  580. // content is actually in the input, so we cannot read its value right
  581. // away. Therefore, the timeouts.
  582. var $el = $(event.target),
  583. word = $el.val(),
  584. tries = 0,
  585. callback;
  586. callback = function () {
  587. var newWord = $el.val();
  588. if (newWord !== word) {
  589. onKeyUp(event);
  590. } else if (tries < 3) {
  591. tries += 1;
  592. setTimeout(callback, 100);
  593. }
  594. };
  595. setTimeout(callback, 100);
  596. };
  597. methods.init = function (settings) {
  598. this.each(function (idx, el) {
  599. // Make it deep extend (first param) so it extends also the
  600. // rules and other inside objects
  601. var clonedDefaults = $.extend(true, {}, defaultOptions),
  602. localOptions = $.extend(true, clonedDefaults, settings),
  603. $el = $(el);
  604. localOptions.instances = {};
  605. $el.data("pwstrength-bootstrap", localOptions);
  606. $.each(localOptions.common.events, function (idx, eventName) {
  607. var handler = eventName === "paste" ? onPaste : onKeyUp;
  608. $el.on(eventName, handler);
  609. });
  610. ui.initUI(localOptions, $el);
  611. $el.trigger("keyup");
  612. if ($.isFunction(localOptions.common.onLoad)) {
  613. localOptions.common.onLoad();
  614. }
  615. });
  616. return this;
  617. };
  618. methods.destroy = function () {
  619. this.each(function (idx, el) {
  620. var $el = $(el),
  621. options = $el.data("pwstrength-bootstrap"),
  622. elements = ui.getUIElements(options, $el);
  623. elements.$progressbar.remove();
  624. elements.$verdict.remove();
  625. elements.$errors.remove();
  626. $el.removeData("pwstrength-bootstrap");
  627. });
  628. };
  629. methods.forceUpdate = function () {
  630. this.each(function (idx, el) {
  631. var event = { target: el };
  632. onKeyUp(event);
  633. });
  634. };
  635. methods.addRule = function (name, method, score, active) {
  636. this.each(function (idx, el) {
  637. var options = $(el).data("pwstrength-bootstrap");
  638. options.rules.activated[name] = active;
  639. options.rules.scores[name] = score;
  640. options.rules.extra[name] = method;
  641. });
  642. };
  643. applyToAll = function (rule, prop, value) {
  644. this.each(function (idx, el) {
  645. $(el).data("pwstrength-bootstrap").rules[prop][rule] = value;
  646. });
  647. };
  648. methods.changeScore = function (rule, score) {
  649. applyToAll.call(this, rule, "scores", score);
  650. };
  651. methods.ruleActive = function (rule, active) {
  652. applyToAll.call(this, rule, "activated", active);
  653. };
  654. $.fn.pwstrength = function (method) {
  655. var result;
  656. if (methods[method]) {
  657. result = methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  658. } else if (typeof method === "object" || !method) {
  659. result = methods.init.apply(this, arguments);
  660. } else {
  661. $.error("Method " + method + " does not exist on jQuery.pwstrength-bootstrap");
  662. }
  663. return result;
  664. };
  665. }(jQuery, methods));
  666. }(jQuery));