gpt4 book ai didi

javascript - 表单验证事件中不会出现失败消息

转载 作者:行者123 更新时间:2023-11-28 05:40:45 25 4
gpt4 key购买 nike

我正在尝试制作一条失败消息,根据提交时的错误将不同的字符串添加在一起。但即使满足条件,也不会出现失败消息。我想知道我的代码错误在哪里。谢谢。

它位于链接到 HTML 文档的外部 JavaScript 文件中。

 function validationEvent() {
var flag = true;
var alertmsg = "There were errors found in your registration:";
var givenname = document.getElementById("gname").value;
var surname = document.getElementById("sname").value;
var address = document.getElementById("address1").value;
var city = document.getElementById("city").value;
var pcode = document.getElementById("pcode").value;
var email = document.getElementById("email").value;
var phone = document.getElementById("phone").value;
var cnum = document.getElementById("cnum").value;
if (givenname == null || givenname == "") {
alertmsg += " Given Name is a mandatory field.";
flag = false;

}
if (surname == null || surname == "") {
alertmsg += " Surname is a mandatory field.";
flag = false;

}
if (address == null || address == "") {
alertmsg += " Address is a mandatory field.";
flag = false;
}
if (city == null || city == "") {
alertmsg += " City is a mandatory field.";
flag = false;
}
if (pcode == null || pcode == "") {
alertmsg += " Postal Code is a mandatory field.";
flag = false;
}
if (email == null || email == "") {
alertmsg += " Email is a mandatory field.";
flag = false;
}
if (phone == null || phone == "") {
alertmsg += " Phone Number is a mandatory field.";
flag = false;
}
if (cnum == null || cnum == "") {
alertmsg += " Credit Card Number is a mandatory field.";
flag = false;
}
if (!vormCredit()) {
alertmsg += " Visa Cards must start with 4 and Mastercard Cards must start with 5.";
flag = false;

}
if (!stateCheck()) {
alertmsg += " Invalid Postal Code.";
flag = false;

}
if (!checkCredit()) {
alertmsg += " Invaid Credit Card Number.";
flag = false;

}
if (!flag) {
alert(alertmsg);
return false;}

else {
alert("Thank you for your subcription.");
return true;}


};

编辑:从“if”循环中删除了“return false”,并将其仅添加到 if(!flag) 循环中。

最佳答案

这里最好使用switch语句。当您有更多案例时,它会处理这种情况。您也可以使用 break

关于javascript - 表单验证事件中不会出现失败消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940698/

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