Page 1 of 1

[JavaScript] Function to check password security.

Posted: Mon Oct 30, 2006 1:17 am
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

Posted: Mon Oct 30, 2006 1:26 am
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".

Posted: Mon Oct 30, 2006 2:30 am
by Chris Corbyn

Posted: Mon Oct 30, 2006 3:11 am
by s.dot
darn, d11 has already done all of my snippets! :-P

yours looks nifty.