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. =]
<!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..
<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>
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.