...

View Full Version : null values - bit of a newbie..



eddjc
08-01-2008, 09:02 AM
Hey guys,

I'm working on a little javascript to do two things - 1) assess dynamically whether the password that the user is typing in is "weak", "medium" or "strong" and then 2) check on clicking "submit" that the passwords match and that they have typed in a password.

My problem is with the first part - I'm using match() to search the password strings for [a-z][A-Z] and \d characters seperately and defining global (g) to return an array of results in the string. There are three attributes that make up my "strong" password - characters, numbers and a length >= 8. However, while typing into the password box, if I for. e.g. type characters and no numbers, my if() statement throws a NULL value and the program exits until I type in both characters and digits.

how can I get round this? Code below. also a live version at http://www.edwardcaine.com/javascript/password.html

Many thanks,

Edd

p.s. code is a little messy sorry - have commented out the actual strength determining code so that I could try and figure it out...


<html>
<head>
</head>
<body>
<form name="setpassword">
<input type="password" name="pass1">
<input type="password" name="pass2">

<textarea name="strength"></textarea>

<input type="submit" value="submit" name="formsubmit"><br />

</form>

<script type="text/javascript">
var p= document.setpassword;

p.onsubmit = function () {

if ((p.pass1.value == undefined && p.pass2.value == undefined) ||
(p.pass1.value == '' && p.pass2.value == '') ||
(p.pass1.value == null && p.pass2.value == null))
{
alert("please enter a password");
return false
}
else if (p.pass1.value != p.pass2.value)
{

// do this if they don't match
alert("Passwords do not match. Please retype");
return false;

}
else {

return true;
}
}

p.pass1.onkeyup = function () {



var chars = p.pass1.value.match(/[a-z]|[A-Z]/g);
var integs = p.pass1.value.match(/\d/g);

var charss =1;
var intss = 1;
var lengths = 1;
var strgth = 1;

var intlength = 0;
var charlength = 0;



if ((chars.length == null) || (chars.length == undefined) || (chars.length == 0))
{ charlength = 0; }
else
{ charlength = chars.length;}

if ((charlength > 0) && (charlength != null) && (charlength != undefined)) charss = 2;

p.strength.value = chars + ": " + charlength;



if (integs.length > 0) {intss = 2;} else {intss = 1};


//strgth = (charss -1) + (intss-1) + (lengths-1);
//p.strength.value = chars + ": " + chars.length + ": " + integs.length +": " + //charss + ": " + strgth + "\n"





//if (p.pass1.value.length >6) {lengths = 2 } else { lengths =1};




//var strarray = ["", "weak", "medium", "strong"];

//p.strength.value = (strgth-1) + ": " + strarray[strgth-1];

}
</script>
</body>
</html>

Philip M
08-01-2008, 09:46 AM
This should help you:-



<input type = "text" id ="pass1" name = "pass1" size = "10" onblur = "chk()">

<script type = "text/javascript">

function chk() {
var ch = document.getElementById("pass1").value
chars = ch.match(/[a-z]/gi);
if (chars == null) {chars = ""};
integs = ch.match(/\d/g);
if (integs == null) {integs = ""}
alert ("The password contains " + chars.length + " alpha-characters and " + integs.length + " digits with a total length of " + ch.length + " characters")
}
</script>


It is your responsibility to die() if necessary… - PHP Manual

eddjc
08-01-2008, 09:52 AM
Awesome! Thanks!


Edd



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum