Testing to see if Javascript Variable Is Set

Discussion in 'Programming' started by tlshaheen, May 26, 2010.

  1. #1
    In the following code, I'm using AJAX to determine whether the passwords entered into a field match. If password2, the confirm field, is empty i.e. they havent entered a password into it yet, I do not want to tested against password 1 - how do i test if password2 is not set? password2 != "" doesnt work.

    function checkPassword() {
    	var password1 = document.getElementById("password").value;
    	var password2 = document.getElementById("password_confirmed").value;
            
    	 if ((password2 != "") && (password1 != password2)){
    		document.getElementById('check_password').innerHTML = "<img src='/images/form_x.png' title='Error' alt='Error' /> <i><b>Passwords Do Not Match!</i></b>";
    		passError = "yes";
    		checkIfError();
    	}
    }
    Code (markup):
     
    tlshaheen, May 26, 2010 IP
  2. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just check for the length of the String

    You can do this with JavaScript. The following example is a demonstration (partially from w3schools.com)
    
    <script type="text/javascript">
    
    var txt = "Hello World!";
    document.write(txt.length);
    if (txt.length == 12) {
      document.write(' Wow!');
    } 
    
    </script>
    
    Code (markup):
     
    flexdex, May 26, 2010 IP
  3. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've tried it with password2.length != 0, but no avail - it passes the test, even when password2 is not set
     
    tlshaheen, May 26, 2010 IP
  4. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    alert your password and look whats inside then:
    
    // hopefully password2 is a string - code not tested
    alert('Content: "' + password2 + '" Length: ' + password2.length);
    
    Code (markup):
     
    flexdex, May 26, 2010 IP
  5. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yep, like I thought: Content comes out as "" so nothing, and length = 0. However the following still goes into the loop, and gives me the alert. What the heck!

    else if ((password2.length != 0) && (password1 != password2)){
    		alert('Content: "' + password2 + '" Length: ' + password2.length);
    		document.getElementById('check_password').innerHTML = "<img src='/images/form_x.png' title='Error' alt='Error' /> <i><b>Passwords Do Not Match!</i></b>";
    		passError = "yes";
    		checkIfError();
    	}
    Code (markup):
     
    tlshaheen, May 26, 2010 IP
  6. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I see that "else if". Could you please post the whole if tree?
     
    flexdex, May 26, 2010 IP
  7. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    function checkPassword() {
    	var password1 = document.getElementById("password").value;
    	var password2 = document.getElementById("password_confirmed").value;
    	/* Print message while checking */
    	document.getElementById('check_password').innerHTML = "Checking...";
    	if((password1 == password2) && (password1.length > 5)){
    				document.getElementById('check_password').innerHTML = "<img src='../images/form_checkmark.png' title='Checkmark' alt='Checkmark' />";;
    				passError = "no";
    				checkIfError();
    	} else if ((password1.length < 6)){
    		document.getElementById('check_password').innerHTML = "<img src='/images/form_x.png' title='Error' alt='Error' /> <i><b>Password must be at least 6 characters!</i></b>";
    		passError = "yes";
    		checkIfError();
    	}
    	else if ((password2.length != 0) && (password1 != password2)){
    		document.getElementById('check_password').innerHTML = "<img src='/images/form_x.png' title='Error' alt='Error' /> <i><b>Passwords Do Not Match!</i></b>";
    		passError = "yes";
    		checkIfError();
    	}
    	else {
    		document.getElementById('check_password').innerHTML = "<img src='/images/form_x.png' title='Error' alt='Error' /> <i><b>Please confirm your password!</i></b>";
    		passError = "yes";
    		checkIfError();	
    	}
    }
    Code (markup):
     
    tlshaheen, May 27, 2010 IP
  8. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    For me everything is fine. Please let us see your FORM with the INPUT fields.

    
    <html>
    <body>
    
    <script type="text/javascript">
    var password1 = '1234567';
    var password2 = '';
    
    if((password1 == password2) && (password1.length > 5)){
      document.write('Fine');
    } else if ((password1.length < 6)){
      document.write('Password must be at least 6 characters!');	
    }
    else if ((password2.length != 0) && (password1 != password2)){
      document.write('Passwords Do Not Match!');	
    }
    else {
      document.write('Please confirm your password!');
    }
    
    </script>
    </body>
    </html>
    
    Code (markup):
     
    flexdex, May 27, 2010 IP
  9. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well the code I have in the last post does work - for some reason, the javascript include wasn't being refreshed, it was still loading the old code. Thanks for your help!
     
    tlshaheen, May 27, 2010 IP
  10. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    While developing with the web i suggest always turn off the browser cache. ;)
     
    flexdex, May 27, 2010 IP
  11. aprillove83

    aprillove83 Peon

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Anyways, try this...('Content: "' + password2 + '" Length: ' + password2.length).
     
    Last edited: May 27, 2010
    aprillove83, May 27, 2010 IP
  12. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    @aprillove83: did you read #4? :)

    Can we please CLOSE this thread?
     
    Last edited: May 27, 2010
    flexdex, May 27, 2010 IP