表单验证工作不正常
Form Validation is not working properly
JavaScript无法正确验证表单。当未输入所需字段时,浏览器不会显示警告框,而是加载到下一页,就像用户输入了字段一样。
<head>
<script type="text/javascript">
function inputValidation() {
var dateBox= document.forms["form"]["date"].value;
if (dateBox== null || dateBox== "") {
alert("Please enter an approximate date.");
return false;
}
}
</script>
</head>
<body>
<form name="form" id="form" action="add.php" onsubmit="inputValidation()" method="post">
<fieldset id="contactFields">
<legend>Client Information</legend><br>
<p id="require"><span>*</span>Required information</p><br>
<table width="291" height="158">
<tr>
<td>Approximate Date Serviced:<span>*</span></td>
<td><input type="text" name="date" id="date" size="20" maxlength="25">
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="fName" id="fName" size="20" maxlength="25" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lName" id="lName" size="20" maxlength="25"/></td>
</tr>
</table>
<label class="blockLabel">Comments:<span>*</span><br><textarea name = "comment" id="comment" rows="5" cols="55"></textarea>
<label id="wordcountLabel" for="wordcount">
<div>Character Count:<input type="text" id="wordcount" readonly="readonly" value="0/0"></div>
</label>
</fieldset>
<div id="submit"><input text-align="center" class="button1" type="submit" value="Submit"></div>
<input text-align="center" class="button3" type="reset" value="Reset" />
</form>
</body>
有什么想法吗?请帮忙!
假设代码正在工作,只有验证不起作用-我猜你想要以下内容:
onsubmit="return inputValidation()"
完整代码:
<head>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function inputValidation()
{
var dateBox= document.forms["form"]["date"].value;
if (dateBox== null || dateBox== "")
{
alert("Please enter an approximate date.");
return false;
}
return true;
}
</script>
</head>
<body>
<form name="form" id="form" action="add.php" onsubmit="return inputValidation()" method="post">
<fieldset id="contactFields">
<legend>Client Information</legend><br />
<p id="require"><span>*</span>Required information</p><br />
<label class="blockLabel">Approximate Date Serviced: <span>*</span><br/><input type="date" name = "date" id="date"/><br/><br/>
<label class="blockLabel">First Name: <span>*</span><br/><input type="text" name = "firstName" id="firstName"/><br/><br/>
<label class="blockLabel">Last Name: <span>*</span><br/><input type="text" name = "lastName" id="lastName"/><br/><br/>
<label class="blockLabel">Comments:<span>*</span><br/><textarea name = "comment" id="comment" rows="5" cols="55"></textarea>
<label id="wordcountLabel" for="wordcount">
<div>Character Count:<input type="text" id="wordcount" readonly="readonly" value="0/0" /></div>
</label>
</fieldset>
<div id="submit"><input text-align="center" class="button1" type="submit" value="Submit" /></div>
<input text-align="center" class="button3" type="reset" value="Reset" />
</form>
</body>
您是否尝试过使用浏览器进行调试并检查dateBox的值??我从来没有见过这样引用的表单值,它可能没有得到你需要的值。
相关文章:
- Javascript.getHours()工作不正常
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 谷歌分析跟踪内部链接不正常工作的事件
- 电话间隙 2.8 位置:固定不正常工作
- GMT . sehours不正常工作
- Jquery幻灯片效果不正常工作
- D3拖动不正常工作
- Jquery搜索字符串从跨度不正常工作
- 美元的范围.在使用MongoDB的controller.js中编辑不正常工作
- 随机音乐播放通过HTML javascript不正常工作
- 华丽的jQuery图像库插件不正常工作,只有在网站的一部分
- Javascript按钮在时钟当前计时不正常工作
- 导航器显示Internet连接状态.联机不正常工作,并显示错误的状态,而互联网没有连接
- jQuery切换不正常工作的第二次
- 谷歌API自动完成不正常工作
- 触摸结束事件不正常工作在铬
- 如何修复HTML5视频Javascript跟踪代码不正常工作
- 为什么第二次按钮点击不正常工作
- 美元.ajax不正常工作在我的代码
- 切换显示的潜水在道场不正常工作在谷歌Chrome浏览器