Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts

    null values - bit of a newbie..

    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...

    Code:
    <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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This should help you:-

    Code:
    <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
    Last edited by Philip M; 08-01-2008 at 09:48 AM.

  • Users who have thanked Philip M for this post:

    eddjc (08-01-2008)

  • #3
    Regular Coder
    Join Date
    Aug 2008
    Posts
    104
    Thanks
    4
    Thanked 14 Times in 14 Posts
    Awesome! Thanks!


    Edd


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •