gpt4 book ai didi

Javascript 表单验证提交功能

转载 作者:行者123 更新时间:2023-12-03 05:43:04 25 4
gpt4 key购买 nike

我的代码中的所有内容都按其应有的方式工作,除了由提交按钮调用的一个函数之外。此函数的目的是在允许提交表单之前再次返回每个验证函数。

最佳答案

验证函数中缺少 return true 语句。如果您不返回任何内容,函数将返回 undefined,该值将被解析为 false;

function nameValidator(name) {
var reg = /^[A-Z][a-z]+$/;
if (reg.test(name)) {
document.getElementById("firstName").style.background = "#8B008B";
return true;
} else {
document.getElementById("firstName").style.background = "#ff5050";
alert("Please capitalize the first letter of name");
return false;
}
}

关于Javascript 表单验证提交功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445472/

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