Password Strength Meter
In this tutorial we will be creating a beautiful password strength indicator. It will determine the complexity of a password and move a meter accordingly with the help of the new
Complexify jQuery plugin. Only when a sufficiently complex password is entered, will the user be able to continue with their registration.
A
PSD file is also available, so you can customize the form to your liking.
The HTML
We start off with a standard HTML5 document that will include our registration form. The form will only serve as an example of the password meter – if you need support for actual registrations, you will need to write the required server-side code.
index.html
04 | < meta charset = "utf-8" /> |
05 | < title >How to Make a Beautiful Password Strength Indicator</ title > |
08 | < link rel = "stylesheet" href = "assets/css/styles.css" /> |
19 | < h1 >Sign up, it's FREE!</ h1 > |
21 | < form class = "" method = "post" action = "" > |
23 | < div class = "row email" > |
24 | < input type = "text" id = "email" name = "email" placeholder = "Email" /> |
27 | < div class = "row pass" > |
28 | < input type = "password" id = "password1" name = "password1" placeholder = "Password" /> |
31 | < div class = "row pass" > |
32 | < input type = "password" id = "password2" name = "password2" placeholder = "Password (repeat)" disabled = "true" /> |
36 | < div class = "arrowCap" ></ div > |
37 | < div class = "arrow" ></ div > |
39 | < p class = "meterText" >Password Meter</ p > |
41 | < input type = "submit" value = "Register" /> |
48 | < script src = "assets/js/jquery.complexify.js" ></ script > |
49 | < script src = "assets/js/script.js" ></ script > |
We are including the latest version of jQuery, the Complexify plugin and our script.js right before the closing body tag for better performance.
jQuery
The jQuery code below is quite straightforward. We are binding a number of events to the form elements and validating them. If an error was encountered, we add an “
error” class to the
.row div that contains the input. This will display the red cross. The “
success” class on the other hand will display a green check mark. When the form is submitted, we check whether all of the rows are marked as successful for allowing the submission.
assets/js/script.js
04 | var pass1 = $( '#password1' ), |
05 | pass2 = $( '#password2' ), |
07 | form = $( '#main form' ), |
08 | arrow = $( '#main .arrow' ); |
11 | $( '#main .row input' ).val( '' ); |
14 | form.on( 'submit' , function (e){ |
17 | if ($( '#main .row.success' ).length == $( '#main .row' ).length){ |
20 | alert( "Thank you for trying out this demo!" ); |
33 | email.on( 'blur' , function (){ |
36 | if (!/^\S+@\S+\.\S+$/.test(email.val())){ |
37 | email.parent().addClass( 'error' ).removeClass( 'success' ); |
40 | email.parent().removeClass( 'error' ).addClass( 'success' ); |
48 | pass2.on( 'keydown input' , function (){ |
51 | if (pass2.val() == pass1.val()){ |
59 | .removeClass( 'success' ) |
With this out of the way, we can move on with the Complexify plugin that will validate the password. The plugin takes a callback function with two arguments – a percentage value from 0 to 100 depending on the complexity of the password, and a
valid flag that takes into account the minimum length requirement, defined by the
minimumChars property.
By tweaking the
strengthScaleFactor property you can allow simpler passwords to be used. The default scale is 1 which requires a mix of upper and lower case letters, numbers and special characters, but I found it too strict so I lowered it to 0.7. You can lower it further if you need even simpler passwords.
01 | pass1.complexify({minimumChars:6, strengthScaleFactor:0.7}, function (valid, complexity){ |
04 | pass2.removeAttr( 'disabled' ); |
11 | pass2.attr( 'disabled' , 'true' ); |
14 | .removeClass( 'success' ) |
18 | var calculated = (complexity/100)*268 - 134; |
19 | var prop = 'rotate(' +(calculated)+ 'deg)' ; |
23 | '-moz-transform' :prop, |
24 | '-webkit-transform' :prop, |
If a valid value has been passed, we will enable the second password field (it is disabled by default). We will also use CSS3 transformations to rotate the arrow. The transformation will be animated thanks to a transition property which you can see in the CSS section. The arrow moves from -134 to 134 degrees (the default 0 degrees correspond to the arrow pointing vertically up), so the code compensates for that.
CSS
I will include only the more interesting parts of the stylesheet here. The code that follows styles the arrow and defines a transition.
assets/css/styles.css
02 | background : url ( "../img/arrow.png" ) no-repeat -10px 0 ; |
12 | -webkit-transition: 0.3 s; |
19 | -moz-transform: rotate( -134 deg); |
20 | -webkit-transform: rotate( -134 deg); |
21 | -o-transform: rotate( -134 deg); |
22 | -ms-transform: rotate( -134 deg); |
23 | transform: rotate( -134 deg); |
28 | background : url ( "../img/arrow.png" ) no-repeat -43px 0 ; |
You can find the rest of the code in
assets/css/styles.css. The best way to learn how it all works is by inspecting the working demo with Firebug or Chrome’s Developer tools and playing with the styles.
We’re done!
You can build upon this example and use it to present an eye catching validation option for your customers. And it is even easier to customize with the included PSD.
0 comments:
Post a Comment