gpt4 book ai didi

javascript - 必填字段的较短验证函数

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:44 25 4
gpt4 key购买 nike

在带有 typescript 的 HTML 应用程序中,我有一个 HTML 表单。此表单有一些必填字段。在我的 typescript 文件中,我正在构建这样的错误消息:

hasErrors() {
let errorCount = 0;

if (!item.Field1()) {
this.ErrorText('"Field1"');
errorCount++;
}
if (!item.Field2()) {
if (errorCount > 0)
this.ErrorText(this.ErrorText() + ', "Field2"');
else
this.ErrorText('"Field2"');
errorCount++;
}
if (!item.Field3()) {
if (errorCount > 0)
this.ErrorText(this.ErrorText() + ', "Field3"');
else
this.ErrorText('"Field3"');
errorCount++;
}

// ...

if (errorCount > 1)
this.ErrorText("The fields " + this.ErrorText() + " are required.";
else if (errorCount == 1)
this.ErrorText("The field " + this.ErrorText() + " is required.";
else
return false;
return true;
}

如您所见,如果需要的字段很多,此函数会变得很长。有没有更好/更短的方法来获得相同的结果?

最佳答案

这将显示默认要求消息

  <form action="includes/loginprocess.php" method="POST">
<input type="text" name="user" placeholder="Username" required>
<input type="password"name="pass" placeholder="Password" required>
<input type="submit" value="Login">
</form>

关于javascript - 必填字段的较短验证函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36405222/

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