Trying to get my page to take a password that is 8 or more characters length and contains a number and a letter, but its not working and I don't understand why. Once the password is valid, it takes the user to a success page.
CODE:
<script>
$(document).ready(function() {
$("#submit").click(function() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var confirm = document.getElementById("confirm").value;
var valid = password.length >= 8 // at least 8 characters
&& /[a-z]/.test(password) // contains a lowercase letter
&& /\d/.test(password) // contains a digit
var dataString = 'name=' + name + '&email=' + email + '&password=' + password;
console.log(dataString);
if (name == '' || email == '' || password == '') {
alert("Fill in empty fields");
}
if (password != confirm) {
alert("Passwords do not match.");
} else if (password == confirm && password != valid) {
alert("Password not valid.");
} else if(password == '' && confirm == '' && password == confirm) {
alert("password can't be empty");
} else {
alert("matched");
window.location="newpage.html"
}
return false;
});
});
</script>
Please help me :(
password != validwhere password is your string and valid is a boolean. Perhaps you should be checkingpassword == confirm && !valid?