表单验证在检查条件后未发出警报
form validation not alerting after checking conditions
我试图用javascript验证HTML表单,但由于没有弹出警报,我认为我的checkSubmit()函数没有被调用。然而,在功能开始时添加了一个额外的警报后,它确实在启动,但在检查第一个条件后不会弹出进一步的警报。
<script language="javascript" type="text/javascript">
function checkSubmit() {
alert("Hello from Javascript!");
if (document.getElementById("user").value.toString.length <1) {
alert("Please enter a user name.");
return false;
}
if (document.getElementById("location").value.toString.length <1) {
alert("Please enter a location.");
return false;
}
if (document.getElementById("depart").value.toString.length <1) {
alert("Please enter a department.");
return false;
}
if (document.getElementById("category").value.toString.length <1) {
alert("Please enter a problem type.");
return false;
}
if (document.getElementById("info").value.toString.length <1) {
alert("Please enter a problem description.");
return false;
}
alert("Hello from Javascript2!");
return true;
}
</script>
<form name="submit" type="submit" method="POST" onsubmit="return checkSubmit();">
[...]
<input type="submit" name="submit" value="Submit" >
</form>
除非我犯了一个错误(我相信事后会很明显),否则不应该弹出第二个警报吗,不管任何条件是真是假?
您不需要toString
,因为.value
已经是一个字符串,而且您使用它的方式不正确——您需要说.toString()
。实际上,它返回函数定义,因此.length
将是0
。正如您所说,您需要为每个框正确设置id
属性。
function checkSubmit() {
if (document.getElementById("user").value.length < 1) {
alert("Please enter a user name.");
return false;
}
if (document.getElementById("location").value.length < 1) {
alert("Please enter a location.");
return false;
}
if (document.getElementById("depart").value.length < 1) {
alert("Please enter a department.");
return false;
}
if (document.getElementById("category").value.length < 1) {
alert("Please enter a problem type.");
return false;
}
if (document.getElementById("info").value.length < 1) {
alert("Please enter a problem description.");
return false;
}
return true;
}
相关文章:
- Javascript中的电子邮件验证检查
- 如何在Netsuite Suitelet脚本中进行验证检查
- 多重验证检查 - 逻辑错误
- jqBootstrapValidation.js中的 Javascript 用户名验证检查
- 禁用对单个非映射选项字段的验证检查
- 推送提交后,如果输入字段为黑色,则显示验证检查或以红色字母显示警告
- Javascript 和 HTML 表单验证检查
- Jquery验证插件,使用选项的数据元素作为验证检查
- 在 C# 和 JavaScript 中对手机号码进行验证检查
- jQuery 验证 - 检查两个密码是否相等
- 使用javascript验证检查将文件上载到文件夹-ASP.NET
- AngularJS - 表单自定义验证 - 检查是否至少有一个输入为空
- jQuery表单验证-检查电子邮件是否相同
- 如何对数据库变量进行健全性和验证检查
- 验证检查输入的长度
- 如何在解析前取消/忽略AngularJS异步验证检查
- 并不是所有的验证检查都在提交时执行
- 只有当验证检查返回true时才通过ajax POST
- Angular的ui-router,可以在其他解决之前解决一个身份验证检查
- javascript验证检查点击条件从HTML