1

我是 Javascript 的新手,正在尝试为登录表单实现 fomr 验证,该表单由名字、姓氏和年龄组成。

实际验证工作正常,但当验证都不满足时,它会继续打印两个错误。我想知道是否可以一次只打印一个错误,而不是两个。

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).");
}
}

任何帮助表示赞赏,在此先感谢。

4

1 回答 1

2

根据我的评论:

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

于 2012-05-23T02:08:06.260 回答