Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2009, 04:43 PM   PM User | #1
nicky
Regular Coder

 
Join Date: May 2008
Location: Michigan
Posts: 216
Thanks: 10
Thanked 1 Time in 1 Post
nicky is on a distinguished road
Password Requirements

I have this hosting application on my website and people just seem oblivious to the fact that right next to the password input it says: "requires one uppercase letter, one lowercase letter, and one number"

I've tried to take the time before to locate a script, but I've never found a good enough tutorial. Can anyone direct me to one? I appreciate it. =]
nicky is offline   Reply With Quote
Old 01-20-2009, 05:06 PM   PM User | #2
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

function meets_password_specifications(pass)
	{
	var lower = "abcdefghijklmnopqrstuvwxyz";
	var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var num = "0123456789";
	var has_lower = false;
	for (var i=0;i<pass.length;i++)
		{
		if (lower.indexOf(pass.charAt(i)) != -1)
			{
			has_lower = true;
			break;
			}
		}
	var has_upper = false;
	for (var i=0;i<pass.length;i++)
		{
		if (upper.indexOf(pass.charAt(i)) != -1)
			{
			has_upper = true;
			break;
			}
		}
	var has_num = false;
	for (var i=0;i<pass.length;i++)
		{
		if (num.indexOf(pass.charAt(i)) != -1)
			{
			has_num = true;
			break;
			}
		}
	return has_lower && has_upper && has_num;
	}

function onFormSubmit(form_elm)
	{
	var pass = form_elm.pass.value;
	var ok = true;
	if (!meets_password_specifications(pass)) ok = false;
	return ok;
	}

// ]]>
</script>
</head>
<body>

<form onsubmit="return onFormSubmit(this)">
	<input type="password" name="pass" /><br />
	<input type="submit" value="submit" />
</form>

</body>
</html>
I used indexof and char strings to check for the 3 specifications because it's actually faster than running toLower/UpperCase(). I also split up the 3 checks in case you want to alert the user of exactly what he did wrong. This is even faster if you don't care:
Code:
function meets_password_specifications(pass)
	{
	var lower = "abcdefghijklmnopqrstuvwxyz";
	var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var num = "0123456789";
	var has_lower=false,has_upper=false,has_num=false;
	for (var i=0;i<pass.length;i++)
		{
		var c = pass.charAt(i);
		if (!has_lower && lower.indexOf(c) != -1) has_lower = true;
		else if (!has_upper && upper.indexOf(c) != -1) has_upper = true;
		else if (!has_num && num.indexOf(c) != -1) has_num = true;
		if (has_lower&&has_upper&&has_num) return true;
		}
	return false;
	}
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com

Last edited by itsallkizza; 01-20-2009 at 05:12 PM..
itsallkizza is offline   Reply With Quote
Old 01-20-2009, 05:21 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Another, perhaps simpler/shorter, way:-

Code:
<script type = "text/javascript">

function chkpwd () {
//var pwd = document.formname.passwordfield.value;
var pwd = "Ab1";  // for testing purposes
if ((/[A-Z]/g.test(pwd)) && (/[a-z]/g.test(pwd)) &&  (/[0-9]/g.test(pwd)) ) {
alert ("Valid Password");
return true;
}
else {
alert ("Invalid Password!\nMust have at least one UPPERCASE letter A-Z\nand one lowercase letter a-z\nand one number 0-9.\nPlease re-enter the password.");
document.formname.passwordfield.value = "" ;  // clear password field
document.formname.passwordfield.focus(); // and refocus on it
return false;
}
}

</script>
You can test your regular expressions at: http://www.claughton.clara.net/regextester.html

Last edited by Philip M; 01-20-2009 at 05:45 PM.. Reason: Improved
Philip M is offline   Reply With Quote
Old 01-20-2009, 06:57 PM   PM User | #4
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
Ran this speed test:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Speed Test</title>
<style type="text/css">
body
{
font-size: 12px;
font-family: arial,sans-serif;
}

form
{
margin: 0;
padding: 0;
}

h2
{
margin: 0;
padding: 12px 0 0 0;
font-size: 18px;
}

.tab
{
margin-right: 20px;
}
</style>
<script type="text/javascript">
// <![CDATA[

/*Author: itsallkizza*/

function func1(pass)
	{
	return ((/[A-Z]/g.test(pass)) && (/[a-z]/g.test(pass)) &&  (/[0-9]/g.test(pass)));
	}

function func2(pass)
	{
	var lower = "abcdefghijklmnopqrstuvwxyz";
	var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var num = "0123456789";
	var has_lower=false,has_upper=false,has_num=false;
	for (var i=0;i<pass.length;i++)
		{
		var c = pass.charAt(i);
		if (!has_lower && lower.indexOf(c) != -1) has_lower = true;
		else if (!has_upper && upper.indexOf(c) != -1) has_upper = true;
		else if (!has_num && num.indexOf(c) != -1) has_num = true;
		if (has_lower&&has_upper&&has_num) return true;
		}
	return false;
	}

function speedTest(iterations)
	{
	var f1_results = document.getElementById("f1_results"),f2_results = document.getElementById("f2_results"),status = document.getElementById("status");
	var t1,t2;
	status.innerHTML = "running";

	var passes = ["abcdefg","Abcdefg","abcDefg","abcdefG","5bcdefg","abc5efg","abcdef5"];

	t1 = (new Date()).getTime();
	for (var i=0;i<iterations;i++) func1(passes[i%passes.length]);
	t1 = (new Date()).getTime()-t1;

	t2 = (new Date()).getTime();
	for (var i=0;i<iterations;i++) func2(passes[i%passes.length]);
	t2 = (new Date()).getTime()-t2;

	status.innerHTML = "test complete";
	f1_results.innerHTML = t1;
	f2_results.innerHTML = t2;
	}

window.onload = function()
	{
	document.getElementById("function1").innerHTML = ((func1+"").replace(/\n/g,"<br />")).replace(/\t/g,'<span class="tab"></span>');
	document.getElementById("function2").innerHTML = ((func2+"").replace(/\n/g,"<br />")).replace(/\t/g,'<span class="tab"></span>');
	}

var dat = new Date();
function onFormSubmit(form_element)
	{
	var i = form_element.iterations.value*1;
	if (i && !isNaN(i)) speedTest(i);
	return false;
	}

// ]]>
</script>
</head>
<body>

<form onsubmit="return onFormSubmit(this)">
	<input type="text" name="iterations" value="25000" /><br />
	<input type="submit" value="start" /><br />
</form>

<h2>Function 1</h2>
<div id="function1"></div>

<h2>Function 2</h2>
<div id="function2"></div>

<h2>Results</h2>
<div id="results">
	<div id="status"></div>
	Function 1 Time: <span id="f1_results"></span><br />
	Function 2 Time: <span id="f2_results"></span>
</div>

</body>
</html>
Philip's is about 5x faster.
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com
itsallkizza is offline   Reply With Quote
Old 01-20-2009, 07:21 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by itsallkizza View Post
Ran this speed test:

Philip's is about 5x faster.
Surprise, surprise! But in any case we are talking about nanoseconds. I don't think speed of execution is much of an issue these days in most situations.
Philip M is offline   Reply With Quote
Old 01-21-2009, 11:44 AM   PM User | #6
nicky
Regular Coder

 
Join Date: May 2008
Location: Michigan
Posts: 216
Thanks: 10
Thanked 1 Time in 1 Post
nicky is on a distinguished road
Thanks a bunch! I'll have to try the scripts later. I appreciate. =]
nicky is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.