[JavaScript] Function to check password security.

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

[JavaScript] Function to check password security.

Post by s.dot »

Code: Select all

//this function will check password security based on a 
//points system
//less than 6 characters = -1 point
//all numerals or all digits = -1 point
//no special characters = -1 point

function check_pass_security(password, updatefield)
{
	var pass = document.getElementById(password).value;
	var score = 0;
	var result;
	
	if(pass.length < 6){
		score = score - 1;
	}
	
	if(!pass.match(/[a-z_]/i) || !pass.match(/[0-9]/)){
		score = score - 1;
	}
	
	if(!pass.match(/\W/)){
		score = score - 1;
	}
	
	if(score == 0){
		result = 'Excellent';
	} else if(score == -1){
		result = 'Good';
	} else if(score == -2){
		result = 'Fair';
	} else if(score == -3){
		result = 'Poor';
	}
	
	document.getElementById(updatefield).innerHTML = result;
	
	return;
}
An example usage!

Code: Select all

<html>
<head>
<title>Password Security Test</title>
<script type="text/javascript">

//this function will check password security based on a 
//points system
//less than 6 characters = -1 point
//all numerals or all digits = -1 point
//no special characters = -1 point

function check_pass_security(password, updatefield)
{
	var pass = document.getElementById(password).value;
	var score = 0;
	var result;
	
	if(pass.length < 6){
		score = score - 1;
	}
	
	if(!pass.match(/[a-z_]/i) || !pass.match(/[0-9]/)){
		score = score - 1;
	}
	
	if(!pass.match(/\W/)){
		score = score - 1;
	}
	
	if(score == 0){
		result = 'Excellent';
	} else if(score == -1){
		result = 'Good';
	} else if(score == -2){
		result = 'Fair';
	} else if(score == -3){
		result = 'Poor';
	}
	
	document.getElementById(updatefield).innerHTML = result;
	
	return;
}

</script>
</head>
<body>
<input id="pass" type="text" name="password" size="20" onkeyup="check_pass_security('pass', 'pwd');" /><br /><br />
Your password security is: <strong><span id="pwd"></span>
</body>
</html>
live demo: http://65.185.136.246/misc/passtest.html
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

when i tried my password it was overcome with joy and cried for so long. The status field showed "You overcome my expectations, Monsieur".
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

darn, d11 has already done all of my snippets! :-P

yours looks nifty.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply