I'm doing a form, and I did a form validation that checks every part of the form but when I need to do multiple errors the code gets really long, the best way will be to use a small example. I have these two input lines:
First Name: <input type="text" id="firstname" name="fname"/>
<p id="firsterror"></p>
Last Name: <input type="text" id="lastname" name="lname" />
<p id="lasterror"></p>
Now, the form will go to the function when I submit it, and the function will be like that for example:
function validateForm() {
var finame = document.forms["myForm"]["fname"].value;
var laname = document.forms["myForm"]["lname"].value;
if ((finame == null || finame == "") && (laname == null || laname == "")) {
document.getElementById("perror").innerHTML = "First name and last name were not filled!";
return false;
}
else if (finame == null || finame == "") {
document.getElementById("perror").innerHTML = "First name was not filled!";
return false;
}
else if (laname == null || laname == "") {
document.getElementById("perror").innerHTML = "Last name was not filled!";
return false;
}
else { return true; } }
Now as you can see there are 3 errors that can happen, when first name is unfilled, last name is unfilled and both are unfilled, now this is just a piece of the code but in the full code I have 7 variables that needs to be checked and this means I need to write a code for all things that can happen for example all vars are unfilled, vars 1-7, vars 2-7, vars 3-7, vars 4-7... and then all the options of 6 vars together and than 5, etc.. This can get too long and someone told me there's such thing that does those options without writing some many code lines, sorry for bad english and bad explainning. thanks in advance :)