My form needs to validate using js and alert any errors then go back to the form with the input values present so they can fix it before re-submitting. Once the validation passes it should redirect to a php "thank you" page. The validation logic is working correctly however after the error alerts it redirects to the php page prematurely.
My form HTML:
<form method = "post" name= "account" action= "validated.php" target="_blank">
<div>
<label for="fname"><span class="red">*</span> First Name:</label><br>
<input type="text" id="fname" name="fname" size="40" required><br>
<label for="lname">Last Name:</label><br>
<input type="text" id="lname" name="lname" size="40">
</div>
<div>
<label for="bdate">Birth Date:</label><br>
<input type="text" id="bdate" name="bdate" placeholder="dd/mm"><br>
<label for="email"><span class="red">*</span> Email Address:</label><br>
<input type="email" id="email" name="email" size="40" placeholder="mymail@mymail.com" required>
</div>
<div>
<p id="interests">Product Interests:</p>
<div class= "pref">
<input type="checkbox" class="preferences" name="interests[]" value="donuts">
<label for="donuts">Donuts</label>
</div>
<div class= "pref">
<input type="checkbox" class="preferences" name="interests[]" value="vanilla slice">
<label for="vanillaSlice">Vanilla Slice</label>
</div>
<div class= "pref">
<input type="checkbox" class="preferences" name="interests[]" value="randy tarts">
<label for="randyTarts">Randy Tarts</label>
</div>
<div class= "pref">
<input type="checkbox" class="preferences" name="interests[]" value="custard tarts">
<label for="custardTarts">Custard Tarts</label>
</div>
<div class= "pref">
<input type="checkbox" class="preferences" name="interests[]" value="raspberry cheesecake">
<label for="raspberryCheesecake">Raspberry Cheesecake</label>
</div>
<div class= "pref">
<input type="checkbox" class="preferences" name="interests[]" value="applecake slice">
<label for="appleCakeSlice">Applecake Slice</label>
</div>
</div>
<div>
<input id="submit" type="submit" value= "Submit" name="submit" onclick="validate()"></input>
</div>
My js code:
var fname, bdate, email;
function validate(){
// Required first name
fname = document.getElementById("fname").value;
try{
if (fname == "") throw "You have not given your first name.";
}
catch(err) {
alert(err);
console.error(err);
}
// valid date format
bdate = document.getElementById("bdate").value;
var contains = bdate.indexOf("/");
var day = bdate.slice(0, 2);
var month = bdate.slice(3);
try{
if (contains != 2 && bdate !== "") throw "The date must be in the following format 'dd/mm' to be valid.";
if (isNaN(day) || isNaN(month) && bdate !== "") throw "The date must contain numbers.";
if (day > 31 && bdate !== "") throw "You seem to have done a typo with your birth day.";
if (month > 12 && bdate !== "") throw "You seem to have done a typo with your birth month.";
}
catch(err) {
alert(err);
console.error(err);
}
// Valid email address
email = document.getElementById("email").value;
var searchAt = email.indexOf("@");
var searchDot = email.indexOf(".");
try{
if (email == "") throw "You have not given your email.";
if (searchAt == -1) throw "Your email must contain an '@' symbol.";
if (searchDot == -1) throw "Your email must contain a . ";
}
catch(err){
alert(err);
console.error(err);
}
};
I would appreciate some help getting the function to go back to the form if validation fails. It works correctly for the name validation but not after the birthday validation. Thanks in advance.