Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | # jQuery Password Strength Meter for Twitter Bootstrap |
| 2 | |||
| 3 | [](https://travis-ci.org/ablanco/jquery.pwstrength.bootstrap) |
||
| 4 | [](https://codeclimate.com/github/ablanco/jquery.pwstrength.bootstrap) |
||
| 5 | [](https://david-dm.org/ablanco/jquery.pwstrength.bootstrap#info=devDependencies) |
||
| 6 | |||
| 7 | The jQuery Password Strength Meter is a plugin for Twitter Bootstrap that |
||
| 8 | provides rulesets for visualy displaying the quality of a users typed in |
||
| 9 | password. |
||
| 10 | |||
| 11 | Dual licensed under the MIT and GPL licenses. |
||
| 12 | |||
| 13 | [jQuery plugins registry entry](http://plugins.jquery.com/pwstrength-bootstrap/) |
||
| 14 | |||
| 15 | |||
| 16 | ## Requirements |
||
| 17 | |||
| 18 | * jQuery 1.7 or higher |
||
| 19 | * Bootstrap 2 or 3 |
||
| 20 | |||
| 21 | |||
| 22 | ## How to use it |
||
| 23 | |||
| 24 | Just invoke the plugin on the password fields you want to attach a strength |
||
| 25 | meter to. For example, to use it on all the password fields with the default |
||
| 26 | examples: |
||
| 27 | |||
| 28 | ```javascript |
||
| 29 | $(':password').pwstrength(); |
||
| 30 | ``` |
||
| 31 | |||
| 32 | To apply it only to one input and change the options: |
||
| 33 | |||
| 34 | ```javascript |
||
| 35 | $('#passwd1').pwstrength({ |
||
| 36 | ui: { showVerdictsInsideProgressBar: true } |
||
| 37 | }); |
||
| 38 | ``` |
||
| 39 | |||
| 40 | |||
| 41 | ## Options |
||
| 42 | |||
| 43 | The plugin expect the options to follow this structure: |
||
| 44 | |||
| 45 | ```javascript |
||
| 46 | options = { |
||
| 47 | common: {}, |
||
| 48 | rules: {}, |
||
| 49 | ui: {} |
||
| 50 | }; |
||
| 51 | ``` |
||
| 52 | |||
| 53 | Let's see the options of each section. |
||
| 54 | |||
| 55 | ### Common |
||
| 56 | |||
| 57 | * __minChar__: |
||
| 58 | |||
| 59 | Default: `6` (Integer) |
||
| 60 | |||
| 61 | Sets the minimum required of characters for a password to not be considered |
||
| 62 | too weak. |
||
| 63 | |||
| 64 | * __usernameField__: |
||
| 65 | |||
| 66 | Default: `"#username"` (String) |
||
| 67 | |||
| 68 | The username field to match a password to, to ensure the user does not use |
||
| 69 | the same value for their password. |
||
| 70 | |||
| 71 | * __userInputs__: |
||
| 72 | |||
| 73 | Default: `[]` (Array) |
||
| 74 | |||
| 75 | Array of CSS selectors for input fields with user input. The content of these |
||
| 76 | fields will be retrieved and passed to the zxcvbn library. |
||
| 77 | |||
| 78 | This option only takes effect when the zxcvbn library is being used. Check |
||
| 79 | the `zxcvbn` option. |
||
| 80 | |||
| 81 | * __onLoad__: |
||
| 82 | |||
| 83 | Default: `undefined` (Function) |
||
| 84 | |||
| 85 | A callback function, fired on load of the widget. No arguments will be |
||
| 86 | passed. |
||
| 87 | |||
| 88 | * __onKeyUp__: |
||
| 89 | |||
| 90 | Default: `undefined` (Function) |
||
| 91 | |||
| 92 | A callback function, fired on key up when the user is typing. The `keyup` |
||
| 93 | event will be passed as first argument, and an object as second with the |
||
| 94 | score and the verdict text and level. |
||
| 95 | |||
| 96 | This handler will also be called when the `change` or the `onpaste` events |
||
| 97 | happen. |
||
| 98 | |||
| 99 | * __zxcvbn__: |
||
| 100 | |||
| 101 | Default: `false` (Boolean) |
||
| 102 | |||
| 103 | Use the zxcvbn to calculate the password entropy and use it as the score. For |
||
| 104 | more information about zxcvbn plase check this |
||
| 105 | [post](https://tech.dropbox.com/2012/04/zxcvbn-realistic-password-strength-estimation/). |
||
| 106 | |||
| 107 | If you activate this setting, then all the rules won't be applied, and all |
||
| 108 | the options under the _Rules_ section will be ignored as well. zxcvbn will be |
||
| 109 | used instead of the default rules engine. |
||
| 110 | |||
| 111 | To use this option you must load the zxcvbn library file on your site. You'll |
||
| 112 | find it at their [repository](https://github.com/lowe/zxcvbn). |
||
| 113 | |||
| 114 | * __debug__: |
||
| 115 | |||
| 116 | Default: `false` (Boolean) |
||
| 117 | |||
| 118 | If true, it prints the password strength in the javascript console, so you |
||
| 119 | can test and tune your rules settings. |
||
| 120 | |||
| 121 | ### Rules |
||
| 122 | |||
| 123 | * __extra__: |
||
| 124 | |||
| 125 | Default: `{}` (Object) |
||
| 126 | |||
| 127 | Empty object where custom rules functions will be stored. |
||
| 128 | |||
| 129 | * __scores__: |
||
| 130 | |||
| 131 | Default: (Object) |
||
| 132 | |||
| 133 | ``` |
||
| 134 | { |
||
| 135 | wordNotEmail: -100, |
||
| 136 | wordLength: -50, |
||
| 137 | wordSimilarToUsername: -100, |
||
| 138 | wordSequences: -50, |
||
| 139 | wordTwoCharacterClasses: 2, |
||
| 140 | wordRepetitions: -25, |
||
| 141 | wordLowercase: 1, |
||
| 142 | wordUppercase: 3, |
||
| 143 | wordOneNumber: 3, |
||
| 144 | wordThreeNumbers: 5, |
||
| 145 | wordOneSpecialChar: 3, |
||
| 146 | wordTwoSpecialChar: 5, |
||
| 147 | wordUpperLowerCombo: 2, |
||
| 148 | wordLetterNumberCombo: 2, |
||
| 149 | wordLetterNumberCharCombo: 2 |
||
| 150 | } |
||
| 151 | ``` |
||
| 152 | |||
| 153 | Scores returned by the rules when they match. Negative values are for |
||
| 154 | penalizations. |
||
| 155 | |||
| 156 | * __activated__: |
||
| 157 | |||
| 158 | Default: (Object) |
||
| 159 | |||
| 160 | ``` |
||
| 161 | { |
||
| 162 | wordNotEmail: true, |
||
| 163 | wordLength: true, |
||
| 164 | wordSimilarToUsername: true, |
||
| 165 | wordSequences: true, |
||
| 166 | wordTwoCharacterClasses: false, |
||
| 167 | wordRepetitions: false, |
||
| 168 | wordLowercase: true, |
||
| 169 | wordUppercase: true, |
||
| 170 | wordOneNumber: true, |
||
| 171 | wordThreeNumbers: true, |
||
| 172 | wordOneSpecialChar: true, |
||
| 173 | wordTwoSpecialChar: true, |
||
| 174 | wordUpperLowerCombo: true, |
||
| 175 | wordLetterNumberCombo: true, |
||
| 176 | wordLetterNumberCharCombo: true |
||
| 177 | } |
||
| 178 | ``` |
||
| 179 | An object that sets wich validation rules are activated. By changing this |
||
| 180 | object is possible to deactivate some validations, or to activate them for |
||
| 181 | extra security. |
||
| 182 | |||
| 183 | * __raisePower__: |
||
| 184 | |||
| 185 | Default: `1.4` (Double) |
||
| 186 | |||
| 187 | The value used to modify the final score, based on the password length, |
||
| 188 | allows you to tailor your results. |
||
| 189 | |||
| 190 | ### User Interface |
||
| 191 | |||
| 192 | * __bootstrap2__: |
||
| 193 | |||
| 194 | Default: `false` (Boolean) |
||
| 195 | |||
| 196 | Sets if it supports legacy Bootstrap 2 (true) or the current Bootstrap 3 |
||
| 197 | (false), the progress bar html is different. |
||
| 198 | |||
| 199 | * __showProgressBar__: |
||
| 200 | |||
| 201 | Default: `true` (Boolean) |
||
| 202 | |||
| 203 | Displays the password strength in a progress bar. |
||
| 204 | |||
| 205 | * __showPopover__: |
||
| 206 | |||
| 207 | Default: `false` (Boolean) |
||
| 208 | |||
| 209 | Displays the error messages and the verdicts in a Bootstrap popover, instead |
||
| 210 | of below the input field. Bootstrap tooltip.js and popover.js must to be |
||
| 211 | included. |
||
| 212 | |||
| 213 | If the `showVerdictsInsideProgressBar` option is active, then the verdicts |
||
| 214 | won't appear on the popover. |
||
| 215 | |||
| 216 | * __showStatus__: |
||
| 217 | |||
| 218 | Default: `false` (Boolean) |
||
| 219 | |||
| 220 | Displays the password strength as a validation status in the password field, |
||
| 221 | for this to work, the Bootstrap form structure must be followed. |
||
| 222 | |||
| 223 | * __spanError__: |
||
| 224 | |||
| 225 | Default: (Function) |
||
| 226 | |||
| 227 | ```javascript |
||
| 228 | function (options, key) { |
||
| 229 | var text = options.ui.errorMessages[key]; |
||
| 230 | return '<span style="color: #d52929">' + text + '</span>'; |
||
| 231 | }; |
||
| 232 | ``` |
||
| 233 | |||
| 234 | Function to generate a span with the style property set to red font color, |
||
| 235 | used in the errors messages. Overwrite for custom styling. |
||
| 236 | |||
| 237 | * __errorMessages__: |
||
| 238 | |||
| 239 | Default: (Object) |
||
| 240 | |||
| 241 | ``` |
||
| 242 | { |
||
| 243 | wordLength: "Your password is too short", |
||
| 244 | wordNotEmail: "Do not use your email as your password", |
||
| 245 | wordSimilarToUsername: "Your password cannot contain your username", |
||
| 246 | wordTwoCharacterClasses: "Use different character classes", |
||
| 247 | wordRepetitions: "Too many repetitions", |
||
| 248 | wordSequences: "Your password contains sequences" |
||
| 249 | } |
||
| 250 | ``` |
||
| 251 | |||
| 252 | An object containing error messages. These can be overwritten for language |
||
| 253 | purposes, and extra messages can also be added for other rules, existing or |
||
| 254 | custom. Use the name of the rule as key. |
||
| 255 | |||
| 256 | * __verdicts__: |
||
| 257 | |||
| 258 | Default: `["Weak", "Normal", "Medium", "Strong", "Very Strong"]` (Array) |
||
| 259 | |||
| 260 | The display names for the verdicts related to the progressClass. It has to |
||
| 261 | have 5 elements, and they would be the 5 possible strength categories. |
||
| 262 | |||
| 263 | * __showVerdicts__: |
||
| 264 | |||
| 265 | Default: `true` (Boolean) |
||
| 266 | |||
| 267 | Determines if the verdicts are displayed or not. |
||
| 268 | |||
| 269 | * __showVerdictsInsideProgressBar__: |
||
| 270 | |||
| 271 | Default: `false` (Boolean) |
||
| 272 | |||
| 273 | Determines if the verdicts are displayed inside the progress bar or not. When |
||
| 274 | this setting is active, the verdict viewport is ignored and they won't appear |
||
| 275 | on the popover if it is being showed. Also this option overrides the value of |
||
| 276 | the _showVerdicts_ one. |
||
| 277 | |||
| 278 | * __showErrors__: |
||
| 279 | |||
| 280 | Default: `false` (Boolean) |
||
| 281 | |||
| 282 | Determines if the error list is displayed with the progress bar or not. |
||
| 283 | |||
| 284 | * __container__: |
||
| 285 | |||
| 286 | Default: `undefined` (CSS selector, or DOM node) |
||
| 287 | |||
| 288 | If defined, it will be used to locate the viewports, if undefined, the parent |
||
| 289 | of the input password will be used instead. The viewports must be children of |
||
| 290 | this node. |
||
| 291 | |||
| 292 | * __viewports__: |
||
| 293 | |||
| 294 | Default: (Object) |
||
| 295 | |||
| 296 | ``` |
||
| 297 | { |
||
| 298 | progress: undefined, |
||
| 299 | verdict: undefined, |
||
| 300 | errors: undefined |
||
| 301 | } |
||
| 302 | ``` |
||
| 303 | |||
| 304 | An object containing the viewports to use to show the elements of the |
||
| 305 | strength meter. Each one can be a CSS selector (`"#progressbar"`) or a DOM |
||
| 306 | node reference. |
||
| 307 | |||
| 308 | * __scores__: |
||
| 309 | |||
| 310 | Default: `[17, 26, 40, 50]` (Array) |
||
| 311 | |||
| 312 | The scores used to determine what progressClass and verdicts to display. It |
||
| 313 | has to have 4 elements, which creates 5 categories of strength (the 5 |
||
| 314 | possible verdicts). |
||
| 315 | |||
| 316 | #### Example of an options object |
||
| 317 | |||
| 318 | ```javascript |
||
| 319 | var options = {}; |
||
| 320 | options.common = { |
||
| 321 | minChar: 8; |
||
| 322 | }; |
||
| 323 | options.rules = { |
||
| 324 | activated: { |
||
| 325 | wordTwoCharacterClasses: true, |
||
| 326 | wordRepetitions: true |
||
| 327 | } |
||
| 328 | }; |
||
| 329 | options.ui = { |
||
| 330 | showErrors: true |
||
| 331 | }; |
||
| 332 | ``` |
||
| 333 | |||
| 334 | |||
| 335 | ## Methods |
||
| 336 | |||
| 337 | Once the plugin has been initialized, it is possible to interact with it |
||
| 338 | through the methods. |
||
| 339 | |||
| 340 | |||
| 341 | ### Force an update |
||
| 342 | |||
| 343 | It is possible to force an update on a password strength meter. It will force |
||
| 344 | a new score calculation and an update of the UI elements, the `onKeyUp` |
||
| 345 | callback will be called. |
||
| 346 | |||
| 347 | ```javascript |
||
| 348 | $("#passwdfield").pwstrength("forceUpdate"); |
||
| 349 | ``` |
||
| 350 | |||
| 351 | |||
| 352 | ### Remove the strength meter |
||
| 353 | |||
| 354 | This will remove the data associated to the meter, and the UI elements. |
||
| 355 | |||
| 356 | ```javascript |
||
| 357 | $("#passwdfield").pwstrength("destroy"); |
||
| 358 | ``` |
||
| 359 | |||
| 360 | |||
| 361 | ### Adding Custom Rules |
||
| 362 | |||
| 363 | The plugin comes with the functionality to easily define your own custom rules. |
||
| 364 | The format is as follows: |
||
| 365 | |||
| 366 | ```javascript |
||
| 367 | $("#passwdfield").pwstrength("addRule", "ruleName", function (options, word, score) {}, rule_score, rule_enabled); |
||
| 368 | ``` |
||
| 369 | |||
| 370 | Example: |
||
| 371 | |||
| 372 | ```javascript |
||
| 373 | $("#passwdfield").pwstrength("addRule", "testRule", function (options, word, score) { |
||
| 374 | return word.match(/[a-z].[0-9]/) && score; |
||
| 375 | }, 10, true); |
||
| 376 | ``` |
||
| 377 | |||
| 378 | |||
| 379 | ### Change the score associated to a rule |
||
| 380 | |||
| 381 | It is possible to change the score given by a rule. It works like this: |
||
| 382 | |||
| 383 | ```javascript |
||
| 384 | $("#passwdfield").pwstrength("changeScore", "wordSequences", -100); |
||
| 385 | ``` |
||
| 386 | |||
| 387 | That would penalize even more the presence of sequences in the password. |
||
| 388 | |||
| 389 | |||
| 390 | ### Activate and deactivate rules |
||
| 391 | |||
| 392 | It is also possible to activate or deactivate rules. It as simple as: |
||
| 393 | |||
| 394 | ```javascript |
||
| 395 | $("#passwdfield").pwstrength("ruleActive", "wordSequences", false); |
||
| 396 | ``` |
||
| 397 | |||
| 398 | That would avoid looking for sequences in the password being tested. |
||
| 399 | |||
| 400 | |||
| 401 | ## Callback Functions |
||
| 402 | |||
| 403 | The plugin provides two callback functions, onLoad and onKeyUp. You can use |
||
| 404 | them like this: |
||
| 405 | |||
| 406 | ```javascript |
||
| 407 | $(document).ready(function () { |
||
| 408 | var options = {}; |
||
| 409 | options.common = { |
||
| 410 | onLoad: function () { |
||
| 411 | $('#messages').text('Start typing password'); |
||
| 412 | }, |
||
| 413 | onKeyUp: function (evt, data) { |
||
| 414 | $("#length-help-text").text("Current length: " + $(evt.target).val().length + " and score: " + data.score); |
||
| 415 | } |
||
| 416 | }; |
||
| 417 | $(':password').pwstrength(options); |
||
| 418 | }); |
||
| 419 | ``` |
||
| 420 | |||
| 421 | |||
| 422 | ## Extra security |
||
| 423 | |||
| 424 | The plugin comes with two validation rules deactivated by default. One checks |
||
| 425 | for too many character repetitions, and the other checks the number of |
||
| 426 | character classes used. An easy way to increase the security of the passwords |
||
| 427 | is to activate this two rules: |
||
| 428 | |||
| 429 | ```javascript |
||
| 430 | $(document).ready(function () { |
||
| 431 | var options = {}; |
||
| 432 | options.rules = { |
||
| 433 | activated: { |
||
| 434 | wordTwoCharacterClasses: true, |
||
| 435 | wordRepetitions: true |
||
| 436 | } |
||
| 437 | }; |
||
| 438 | $(':password').pwstrength(options); |
||
| 439 | }); |
||
| 440 | ``` |
||
| 441 | |||
| 442 | |||
| 443 | ## Examples |
||
| 444 | |||
| 445 | There are some examples in the `examples` directory. Just serve them with any |
||
| 446 | webserver and check them in your browser. Make sure you serve the `examples` |
||
| 447 | directory as the site root. For example: |
||
| 448 | |||
| 449 | ```bash |
||
| 450 | cd examples |
||
| 451 | python -m SimpleHTTPServer |
||
| 452 | ``` |
||
| 453 | |||
| 454 | And go to [localhost:8000](http://localhost:8000). |
||
| 455 | |||
| 456 | |||
| 457 | ## Build and Test |
||
| 458 | |||
| 459 | The build and testing processes rely on [Grunt](http://gruntjs.com/). To use |
||
| 460 | them you need to have [node.js](http://nodejs.org/) and grunt-cli installed on |
||
| 461 | your system. Assuming you have node.js in your Linux system, you'll need to do |
||
| 462 | something like this: |
||
| 463 | |||
| 464 | ```bash |
||
| 465 | sudo npm install -g grunt-cli |
||
| 466 | ``` |
||
| 467 | |||
| 468 | Now you have the grunt command line utility installed globally. |
||
| 469 | |||
| 470 | |||
| 471 | ### Bundle and minified |
||
| 472 | |||
| 473 | To generate the bundle and the minified file you only need to execute this in |
||
| 474 | the project directory: |
||
| 475 | |||
| 476 | ```bash |
||
| 477 | npm install -d |
||
| 478 | grunt |
||
| 479 | ``` |
||
| 480 | |||
| 481 | It will check the source files, and build a minified version with its |
||
| 482 | corresponding source map. The generated files will be available in the `dist` |
||
| 483 | directory. |
||
| 484 | |||
| 485 | |||
| 486 | ### Testing |
||
| 487 | |||
| 488 | To run the tests the only thing you need to do is execute this in the project |
||
| 489 | directory: |
||
| 490 | |||
| 491 | ```bash |
||
| 492 | npm install -d |
||
| 493 | grunt test |
||
| 494 | ``` |
||
| 495 | |||
| 496 | It will check all the source files with [JSLint](http://jslint.com) and run the |
||
| 497 | tests, which are written with [Jasmine](http://jasmine.github.io/). You'll find |
||
| 498 | the tests source code in the `spec` directory. |
||
| 499 | |||
| 500 | [Travis](https://travis-ci.org/ablanco/jquery.pwstrength.bootstrap) is being |
||
| 501 | used for continuos integration. You can check there if the tests are passing. |