gpt4 book ai didi

javascript - 使用表单验证 - 多个错误显示

转载 作者:行者123 更新时间:2023-11-30 18:26:17 25 4
gpt4 key购买 nike

我是 Javascript 的新手,正在尝试对包含名字、姓氏和年龄的登录表单实现表单验证。

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

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

感谢任何帮助,提前致谢。

最佳答案

根据我的评论:

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

关于javascript - 使用表单验证 - 多个错误显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com