I'm sorta new to Javascript and am trying to implement fomr validation for a login form, which consists of first name, surname and age.
The actual validation is working fine, but it keeps printing both errors when none of the validation is satisfied. I want to know if it's possible to only print one error at a time, not both.
function Validation() {
// Basic form validation for the login form (ensures first name is entered ensures age is of required number [5-100])
// Declare the textfields as three seperate variables x, y, z
var x = document.forms["loginform"]["age"].value;
var y = document.forms["loginform"]["firstname"].value;
var z = document.forms["loginform"]["surname"].value;
// Call an error if an input has no entered value (empty)
if (x == null || x == "" || y == null || y == "" || z == null || z == "")
{
alert("Please enter all required information!");
}
// Call an error if x (age) contains a non-numeral character or if the number is less than 5/more than 100
if (x < 5 || x > 100 || !x.match(/^\d+$/))
{
alert("Age must be between 5-100 and only contain numerical value(s).");
}
}
Any help is appreciated, thanks in advance.
else if (x < 5 || x > 100 || !x.match(/^\d+$/))