我是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).");
}
}
提前感谢任何帮助。
答案 0 :(得分:2)
根据我的评论:
添加其他:else if (x < 5 || x > 100 || !x.match(/^\d+$/))