PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/common/libraries/plugin/jquery/jquery.jpassword.js

https://bitbucket.org/chamilo/chamilo/
JavaScript | 178 lines | 126 code | 10 blank | 42 comment | 129 complexity | e18ebf62509fa5828d3a28d4993db128 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. /**
  2. * jQuery jPassword plugin
  3. * @licenses Creative Commons BY-SA [ http://creativecommons.org/licenses/by-sa/2.0/deed.fr ]
  4. *
  5. * @name jPassword
  6. * @desc Plugin jQuery that provides you to detect strength of password. It gives help messages to write a strong password and generates on demand personnalized password. Help can be localized and shown in a tooltip or directly after input.
  7. * @author Herv?Š GOUCHET [ contact(at)rvdevsign(dot)net ]
  8. * @version 1.0
  9. * @date 2009/01/24
  10. * @doc http://www.rvdevsign.net/ressources/javascript/jpassword-plugin-jquery.html
  11. * @requires jQuery v1.2.6+
  12. *
  13. * Example:
  14. * $("input").jpassword();
  15. */
  16. (function($){
  17. $.fn.jpassword = function(settings){
  18. var jElements = this;
  19. var settings = $.extend({}, $.fn.jpassword.defaults, settings);
  20. var template = '<div class="jpassword"><div style="padding: 0px; margin: 0px;"><p class="jpassword-meter">&nbsp;</p><p class="jpassword-info">&nbsp;</p></div></div>';
  21. return jElements.each(function(){
  22. // Manage all inputs type password
  23. if($(jElements).is("input")){ jPassword( $(jElements) ); }
  24. });
  25. // Construct password meter
  26. function jPassword(jInput){
  27. // Create tooltip
  28. var unikId = "jpassword_" + parseInt(Math.random()*1000);
  29. var jTooltip = $(template).attr("id", unikId);
  30. if(settings.flat == false){
  31. // Define position of the tooltip
  32. var pos = jInput.offset();
  33. var win = getWindow();
  34. var dir = "right";
  35. var top = pos.top;
  36. var left = (pos.left + jInput.width());
  37. jTooltip.appendTo(document.body);
  38. if((left + jTooltip.width()) > (win.left + win.width)){ left -= (jTooltip.width() + jInput.width()); dir = "left"; }
  39. if((top + jTooltip.height()) > (win.top + win.height)){ top -= (jTooltip.height() - (jInput.height()*1.5)); dir += "bottom"; }else{ dir += "top"; }
  40. jTooltip.css({ left: left + "px", top: top + "px", display: "none" });
  41. jTooltip.addClass("jpassword-" + dir);
  42. }else{
  43. // Insert after the input
  44. jTooltip.insertAfter(jInput);
  45. jTooltip.css({ position: "relative", display: "none" });
  46. jTooltip.addClass("jpassword-flat");
  47. }
  48. // Event handler
  49. jInput.bind("keyup", function(e){ verifPsw(jInput, jTooltip); });
  50. jInput.bind("focus", function(e){
  51. verifPsw(jInput, jTooltip);
  52. // Show tooltip
  53. if(settings.flat == false){ tooltip(jTooltip, "show"); }
  54. // Function called when the tooltip is shown
  55. if($.isFunction(settings.onShow)){ settings.onShow(jInput, jTooltip); }
  56. });
  57. jInput.bind("blur", function(e){
  58. // Hide tooltip
  59. if(settings.flat == false){ tooltip(jTooltip, "hide"); }
  60. // Function called when the tooltip is hided
  61. if($.isFunction(settings.onHide)){ settings.onHide(jInput, jTooltip); }
  62. });
  63. // Generate a new password
  64. var jGenerate = $("#" + settings.generate);
  65. if(jGenerate){ jGenerate.bind("click", function(e){ jInput.val(newPsw()); verifPsw(jInput, jTooltip); return false; }); }
  66. // Function called when process is completed
  67. if($.isFunction(settings.onComplete)){ settings.onComplete(jInput, jTooltip); }
  68. }
  69. // Verified password and update the tolerance meter
  70. function verifPsw(jInput, jTooltip){
  71. var val = jInput.val();
  72. var meter = jTooltip.find(".jpassword-meter");
  73. var info = jTooltip.find(".jpassword-info");
  74. var psw = securPsw(val);
  75. // Advises for write a strong password
  76. var msg = "";
  77. if(psw.lowercase < 2){
  78. msg = settings.lang.lowercase;
  79. }else if(psw.uppercase < 2){
  80. msg = settings.lang.uppercase;
  81. }else if(psw.number < 2){
  82. msg = settings.lang.number;
  83. }else if(psw.punctuation < 2){
  84. msg = settings.lang.punctuation;
  85. }else if(psw.special < 2){
  86. msg = settings.lang.special;
  87. }
  88. // Correct length ?
  89. if(val.length < settings.length && psw.level < 10 && msg == ""){ msg = settings.lang.length.replace(/-X-/g, settings.length); }
  90. // Display of level
  91. if(psw.val == ""){
  92. meter.css("background-position", "0 0");
  93. info.html(settings.lang.please);
  94. }else if(psw.level < 5){
  95. meter.css("background-position", "0 -10px");
  96. info.html(settings.lang.low + " " + msg);
  97. }else if(psw.level < 10){
  98. meter.css("background-position", "0 -20px");
  99. info.html(settings.lang.correct + " " + msg);
  100. }else{
  101. meter.css("background-position", "0 -30px");
  102. info.html(settings.lang.high);
  103. }
  104. // Replace value of password
  105. jInput.val(psw.val);
  106. // Function called when writing the password
  107. if($.isFunction(settings.onKeyup)){ settings.onKeyup(jInput); }
  108. }
  109. // Verified degree of security of password
  110. function securPsw(val){
  111. val = val.replace(/(^\s+)|(\s+$)/g, "");
  112. var cNbr = cCap = cMin = cPct = cSpe = 1;
  113. var len = val.length;
  114. for(var c = 0; c < len; c++){
  115. var char = val.charCodeAt(c);
  116. if(char < 128){ if(char > 47 && char < 58){ cNbr += 1; }else if(char > 64 && char < 91){ cCap += 1; }else if(char > 96 && char < 123){ cMin += 1; }else{ cPct += 2;} }else{ cSpe += 3; }
  117. }
  118. var lPsw = (cNbr * cCap * cMin * cPct * cSpe);
  119. lPsw = Math.round(Math.log((lPsw * lPsw)));
  120. return { val: val, level: lPsw, number: cNbr, uppercase: cCap, lowercase: cMin, punctuation: cPct, special: cSpe };
  121. }
  122. // Generate a password
  123. function newPsw(){
  124. var val = "";
  125. for(c = 0; c < settings.length; c++){
  126. var char = Math.round(32+Math.random()*222);
  127. var ok = 0;
  128. // Number
  129. if((char > 47 && char < 58) || (char > 64 && char < 91) || (char > 96 && char < 123)){ ok = 1; }
  130. // Upper or lower case
  131. if(settings.type == 1 && char < 127){ ok = 1; }
  132. // Puntuations
  133. if(settings.type == 2){ ok = 1; }
  134. // Special
  135. if(settings.special && (char == 48 || char == 49 || char == 50 || char == 53 || char == 54 || char == 56 || char == 57 || char == 66 || char == 67 || char == 68 || char == 71 || char == 73 || char == 75 || char == 79 || char == 80 || char == 81 || char == 83 || char == 85 || char == 86 || char == 87 || char == 88 || char == 90 || char == 99 || char == 104 || char == 105 || char == 107 || char == 108 || char == 111 || char == 112 || char == 113 || char == 115 || char == 117 || char == 118 || char == 119 || char == 120 || char == 122)){ ok = 0; }
  136. if(ok == 1){ val += String.fromCharCode(char); }else{ c--; }
  137. }
  138. return val;
  139. }
  140. // Show or hide tooltip
  141. function tooltip(jTooltip, effect){
  142. if(effect == "show"){ jTooltip.fadeIn(); }else{ jTooltip.fadeOut(); }
  143. }
  144. // Get window size
  145. function getWindow(){
  146. var m = document.compatMode == "CSS1Compat";
  147. return {
  148. left : (window.pageXOffset || (m ? document.documentElement.scrollLeft : document.body.scrollLeft)),
  149. top : (window.pageYOffset || (m ? document.documentElement.scrollTop : document.body.scrollTop)),
  150. width : (window.innerWidth || (m ? document.documentElement.clientWidth : document.body.clientWidth)),
  151. height : (window.innerHeight || (m ? document.documentElement.clientHeight : document.body.clientHeight))
  152. };
  153. }
  154. };
  155. // Default settings
  156. $.fn.jpassword.defaults = {
  157. lang: { please: "A strong password...", low: "Low security.", correct: "Correct security.", high: "High security.", length: "-X- characters would be a plus.", number: "Why not numbers?", uppercase: "And caps?", lowercase: "Some tiny?", punctuation: "Punctuations?", special: "Best, special characters?" },
  158. length: 8, // Length minimal of good password
  159. flat: false, // Add jPassword after input or show it on demand
  160. type: 1, // 0: low, 1: correct, 2: high. Defined level of security
  161. special: 0, // 0 or 1. If 1, used the special chars when generating password
  162. generate: null, // ID of the element whose on click generates a password (without #)
  163. onShow: function(){}, // Function called when the tooltip is shown (return: jQuery of input and tooltip)
  164. onHide: function(){}, // Function called when the tooltip is hided (return: jQuery of input and tooltip)
  165. onKeyup: function(){}, // Function called when writing the password (return: jQuery of input)
  166. onComplete: function(){} // Function called when the process is done (return: jQuery of input and tooltip)
  167. };
  168. })(jQuery);