Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
/*
This expects a HTML page with two fields, password, and password_feedback.
every time the password text field changes, it rewrites the contents of the
password_feedback field to show the feedback. For example, try this:

		<input id="password" type="password" name="my_password"/>
		<span id="password_feedback"></span>
		<script type=\"text/javascript\" src=\"scripts/password.js\"></script>
 */

var passwordField1;
var passwordField2;

function passwordSetup(pass1, pass2) {
	passwordField1 = pass1;
	passwordField2 = pass2;

	document.getElementById('password_feedback').innerHTML = '';
	document.getElementById(passwordField1).onchange = passwordChange;
	document.getElementById(passwordField1).onkeypress = passwordChange;
	document.getElementById(passwordField1).onkeydown = passwordChange;
	document.getElementById(passwordField1).onkeyup = passwordChange;
}

var commonPasswords = [
	"000000", "102030", "1111", "111111", "11111111", "112233", "121212",
	"123123", "123123123", "123321", "1234", "12345", "123456", "1234567",
	"12345678", "123456789", "1234567890", "123654", "123qwe", "1q2w3e",
	"1q2w3e4r", "1qaz2wsx", "2000", "222222", "555555", "654321", "666666",
	"6969", "696969", "753951", "7777777", "987654321", "aaaaaa", "abc",
	"abc123", "abcd1234", "abcdef", "access", "alexander", "amanda", "andrea",
	"andrew", "asdasd", "asdfasdf", "asdfgh", "asdfghj", "asdfghjkl", "ashley",
	"asshole", "austin", "azerty", "baseball", "batman", "biteme", "buster",
	"charlie", "cheese", "chocolate", "computer", "corvette", "dallas",
	"daniel", "dragon", "dreamweaver", "fdsa", "football", "freedom", "fuck",
	"fucker", "fuckme", "fuckyou", "george", "ginger", "golfer", "hammer",
	"hannah", "harley", "heather", "hello", "hockey", "hunter", "iloveyou",
	"internet", "jennifer", "jessica", "jesus", "jordan", "joshua", "killer",
	"letmein", "liverpool", "love", "macromedia", "maggie", "master",
	"matrix", "matthew", "merlin", "michael", "michelle", "monkey", "mustang",
	"nicole", "ninja", "orange", "pass", "password", "password1", "pepper",
	"photoshop", "princess", "purple", "pussy", "qazwsx", "qwerty",
	"qwertyuiop", "ranger", "robert", "samsung", "secret", "sexy",
	"shadow", "silver", "snoopy1", "soccer", "starwars", "summer",
	"sunshine", "superman", "taylor", "test", "thomas", "thunder",
	"tigger", "trustno1", "welcome", "whatever", "william", "yankees",
	"zxcvbnm"];


function passwordChange() {

	password = document.getElementById(passwordField1).value;

	if(password.length == 0) {
		document.getElementById('password_feedback').innerHTML='';
		return true;
	}

	for(var i = 0; i < commonPasswords.length; i++) {
		if(commonPasswords[i].toUpperCase() === password.toUpperCase()) {
			document.getElementById('password_feedback').innerHTML='  <em>You have entered one of the top 100 most common passwords. </em>';
			return true;
		}
	}

	if(/abc/.test(password)) {
		document.getElementById('password_feedback').innerHTML='  <em>We recommend your password should not contain "abc". </em>';
		return true;
	}
	if(/123/.test(password)) {
		document.getElementById('password_feedback').innerHTML='  <em>We recommend your password should not contain "123". </em>';
		return true;
	}
	if(password.length < 8) {
		document.getElementById('password_feedback').innerHTML='  <em>Please choose a longer password. </em>';
		return true;
	}
	if(password.length < 16 && /^[A-Za-z ]*$/.test(password)) {
		document.getElementById('password_feedback').innerHTML='  <em>Please choose a longer password, or include a number or special character. </em>';
		return true;
	}

	if(password.length > 16 && !/^[A-Za-z ]*$/.test(password)) {
		document.getElementById('password_feedback').innerHTML='';
		return true;
	}

	document.getElementById('password_feedback').innerHTML='';
	return true;
}

New to GrepCode? Check out our FAQ X