This is my javascript but it works fine with all the other fields when nothing is entered I'm trying to make the user need to enter at least 8 characters before submitting but currently, they can submit on 0.
function validateForm() {
if (document.register.firstName.value == "") {
alert("Please provide your first name!");
document.register.firstName.focus();
return false;
}
if (document.register.secondName.value == "") {
alert("Please provide your second name!");
document.register.secondName.focus();
return false;
}
if (document.register.email.value == "") {
alert("Please provide your Email!");
document.register.email.focus();
return false;
}
if (document.register.phoneNo.value == "") {
alert("Please provide your Phone Number!");
document.register.phoneNo.focus();
return false;
}
if (document.register.Gender.value == "") {
alert("Must select a Gender!");
document.register.Gender.focus();
return false;
}
if (document.register.Terms.value == "") {
alert("Terms and Conditions must be accpeted");
document.register.Terms.focus();
return false;
}
if (document.register.psw.value.length > 7) {
alert("Password must contain 8 character");
document.register.psw.focus();
return false;
}
return (true);
}
You need to check if it's <= 7 characters in length (or probably < 8 for clarity). Currently you're preventing the user from submitting a password if it's too long, not the other way around:
if (document.register.psw.value.length < 8) {
...
}
I'm assuming you're using the password input field. If so, you can handle that from the HTML by doing something like this (see below) and have the browser handle the validation.
<input type="password" minlength="4" maxlength="8">
Just try the following. Notice, as you mentioned, it is not greater than 7, but less than 8.
if( document.register.psw.value.length < 8) {
alert( "Password must contain 8 character" );
document.register.psw.focus() ;
return false;
}