I need to add some further validation to this password code. I created a if statement where if any of the conditions fail, the condition that failed will print out the relevant message. I tried adding validation for:
- Password must have 1 Digit (0-9)
- Password must have 1 UpperCase (A-Z)
- Password must have 1 LowerCase (a-z)
Javascript code:
<script type=text/javascript>
function check_form() {
var passw = document.getElementById('password-input-0').value;
var passw2 = document.getElementById('password-input-1').value;
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
if (passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw)) {
if (passw.length < 6) {
alert(Please make sure password is longer than 6 characters.)
return false;
}
if (passw != passw2) {
alert(Please make sure passwords match.)
return false;
}
if (!letter.test(passw)) {
alert(Please make sure Password Includes an UpperCase and LowerCase character)
return false;
}
if (!number.test(passw)) {
alert(Please make sure Password Includes a Digit)
return false;
}
/*email test*/
var email = document.getElementById('email-input-0').value;
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email)) {
alert('Please provide a valid email address');
form.email.focus;
return false;
}
return true;
}
}
</script>
This code doesn't seem to work and being an inexperience JS programmer I'm not sure why it's not working. I've left the email-validation just to make sure the fix doesn't interfere with it. Thanks for reading!