All pastes #627802 Raw Edit

Password Meter

public javascript v1 · immutable
·published 2007-07-20 15:02 UTC
rendered paste body
/* jQuery Password Strength Plugin (pstrength) - A jQuery plugin to provide accessibility functions * Author: Tane Piper (digitalspaghetti@gmail.com)  * Website: http://code.google.com/p/ability/ * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php *  * === Changelog === * Version 1.0 (20/07/2007) * Initial version. */(function($) {		function checkPassword(passwd, settings)	{		var intScore = 0;		var strVerdict = settings.verdects[0];				// PASSWORD LENGTH		if (passwd.length<5)                         // length 4 or less		{			intScore = (intScore + 3)		}		else if (passwd.length>4 && passwd.length<8) // length between 5 and 7		{			intScore = (intScore+6)		}		else if (passwd.length>7 && passwd.length<16)// length between 8 and 15		{			intScore = (intScore+12)		}		else if (passwd.length>15)                    // length 16 or more		{			intScore = (intScore+18)		}		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)		if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter		{			intScore = (intScore+1)		}		if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter		{			intScore = (intScore+5)		}		// NUMBERS		if (passwd.match(/\d+/))                                 // [verified] at least one number		{			intScore = (intScore+5)		}		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers		{			intScore = (intScore+5)		}		// SPECIAL CHAR		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character		{			intScore = (intScore+5)		}		// [verified] at least two special characters		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))		{			intScore = (intScore+5)		}		// COMBOS		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case		{			intScore = (intScore+2)		}		if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers		{			intScore = (intScore+2)		} 		// [verified] letters, numbers, and special characters		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))		{			intScore = (intScore+2)		}		console.log(intScore);		return intScore;	} 	// Runs password through check and then updates GUI 	function runPassword(strPassword, strFieldID, settings) 	{		// Check password		nPerc = checkPassword(strPassword, settings);		 	// Get controls    	var ctlBar = document.getElementById(strFieldID + "_bar");     	var ctlText = document.getElementById(strFieldID + "_text");    	if (!ctlBar || !ctlText)    		return;    	// Set new width    	var nRound = Math.round(nPerc * 100);		if (nRound < (strPassword.length * 5)) 		{ 			nRound += strPassword.length * 5; 		}		if (nRound > 100)			nRound = 100;    		ctlBar.style.width = nRound + "%";		// Color and text						if(nPerc < 16)		{		   	strColor = "red";	 		strText = settings.verdects[0];		}		else if (nPerc > 15 && nPerc < 25)		{		   	strColor = "#ffd801";	 		strText = settings.verdects[1];		}		else if (nPerc > 24 && nPerc < 35)		{		   	strColor = "#ffd801";	 		strText = settings.verdects[2];		}		else if (nPerc > 34 && nPerc < 45)		{		   	strColor = "orange";	 		strText = settings.verdects[3];		}		else		{		   	strColor = "#3bce08";	 		strText = settings.verdects[4];		}		ctlBar.style.backgroundColor = strColor;		ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";	} 	// Checks a string for a list of characters	function doesContain(strPassword, strCheck)	{    	nCount = 0;  		for (i = 0; i < strPassword.length; i++) 		{			if (strCheck.indexOf(strPassword.charAt(i)) > -1) 			{ 	        	nCount++; 			} 		}  		return nCount; 	} 			$.fn.extend({	/* pstrength: function(settings)	 * The constructor method	 * Example: $().pstrength();	 */		pstrength: function(settings) {			var version = "0.1";			/* Default Settings*/				settings = jQuery.extend({				verdects: ['very weak', 'weak', 'medium', 'strong', 'stronger']			},settings);					return this.each(function(){				passfield = this;							jQuery(this).keyup(function(){						runPassword(jQuery(passfield).attr('value'), passfield.id, settings);				});							});		}	});})(jQuery);