1

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.

2
  • add else: else if (x < 5 || x > 100 || !x.match(/^\d+$/)) Commented May 23, 2012 at 2:05
  • Thank you so much! I had a feeling I was close... works perfectly now :) Commented May 23, 2012 at 2:06

1 Answer 1

2

As per my comment:

add else: else if (x < 5 || x > 100 || !x.match(/^\d+$/))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.