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):
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):
I've tried it with password2.length != 0, but no avail - it passes the test, even when password2 is not set
alert your password and look whats inside then: // hopefully password2 is a string - code not tested alert('Content: "' + password2 + '" Length: ' + password2.length); Code (markup):
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):
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):
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):
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!