作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在带有 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/
我是一名优秀的程序员,十分优秀!