Luke Holland Luke Holland - 28 days ago 8
HTML Question

HTML/JavaScript Validation Issues

Part of a website I'm building I'm trying to validate the registration from. The code works it's just once it gets to validating the firstname it seems to quit and return true.. I'm lost on how to fix it as I haven't encountered this issue before and I cannot see anything wrong with my code..



function validateForm(){

var x = document.registration.email.value;
var atPos = x.indexOf("@");
var dotPos = x.lastIndexOf(".");

if(document.registration.user.value.length < 4){
window.alert("Please enter a valid username. (4 - 20 Characters)");
document.registration.user.focus();
return false;
}

if(document.registration.pass.value.length < 4){
window.alert("Please provide a valid password. (4 - 50 Characters)");
document.registration.pass.focus();
return false;
}

if(atPos < 1 || dotPos < atPos + 2 || dotPos + 2 >= x.length) {
window.alert("Invalid email format.");
document.registration.email.focus();
return false;
}

if(document.registration.firstname.value.length < 3){
window.alert("Please provide a valid firstname. (3 - 20 Characters)");
document.registration.fistname.focus();
return false;
}

if(document.registration.surname.value.length < 3){
window.alert("Please a valid surname. (3 - 20 Characters)");
document.registration.surname.focus();
return false;
}

if(document.registration.country.value.length < 3){
window.alert("Please provide valid Country. (3 - 20 Characters)");
document.registration.country.focus();
return false;
}else{
return true;
}
}

<form name="registration" action="registration.php" method="post" onsubmit="return validateForm()">




Answer Source

You have a typo for the firstname field:

document.registration.fistname.focus();

You can check your browser's element inspector for any javascript errors after submitting the form.