问题一部分代码:问题在于判断不执行
var time=window.document.form["equip.buytime"];
var r = time.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)
{
alert("请以2000-10-10格式输入时间");
return false;
}
var price=window.document.form["equip.price"];
re=/^(([1-9]\d*)|0)(\.\d{1,2})?$/
if(!re.test(price))
{
alert("请以2000.00格式输入价格");
return false;
}
<th align="center" valign="middle" nowrap="nowrap">购买时间:<br /></th>
<td align="left" valign="middle"><input name="equip.buytime" type="text" size="50"/></td>
<td valign="middle">2009-10-10</td>
</tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap">价格:<br /></th>
<td align="left" valign="middle"><input name="equip.price" type="text" size="50"/></td>
<td valign="middle">*2000.00 保留小数点后两位</td>
</tr>问题二部分代码:问题也是不能执行 而且从else以后的一个if子句在eclipse中居然变成了绿色 而一模一样的判断写在别的页面没有问题
<script type="text/javascript">
function checkform()
{
if(window.document.form["staff.staffid"].value.length==0)
{
alert("员工编号不能为空");
return false;
}
else
if(window.document.form["staff.staffname"].value.length==0)
{
alert("姓名不能为空");
return false;
}
}</script>
<style type="text/css">
<form name="form" action="AddStaff.action" method="post" onsubmit="return checkform()">
<table width="25%" align="center">
<tbody>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>员工编号:<br/><br/></th>
<td><input type="text" name="staff.staffid"/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>姓名:<br/><br/></th>
<td><input type="text" name="staff.staffname"/></td></tr>
<tr>
var time=window.document.form["equip.buytime"];
var r = time.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)
{
alert("请以2000-10-10格式输入时间");
return false;
}
var price=window.document.form["equip.price"];
re=/^(([1-9]\d*)|0)(\.\d{1,2})?$/
if(!re.test(price))
{
alert("请以2000.00格式输入价格");
return false;
}
<th align="center" valign="middle" nowrap="nowrap">购买时间:<br /></th>
<td align="left" valign="middle"><input name="equip.buytime" type="text" size="50"/></td>
<td valign="middle">2009-10-10</td>
</tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap">价格:<br /></th>
<td align="left" valign="middle"><input name="equip.price" type="text" size="50"/></td>
<td valign="middle">*2000.00 保留小数点后两位</td>
</tr>问题二部分代码:问题也是不能执行 而且从else以后的一个if子句在eclipse中居然变成了绿色 而一模一样的判断写在别的页面没有问题
<script type="text/javascript">
function checkform()
{
if(window.document.form["staff.staffid"].value.length==0)
{
alert("员工编号不能为空");
return false;
}
else
if(window.document.form["staff.staffname"].value.length==0)
{
alert("姓名不能为空");
return false;
}
}</script>
<style type="text/css">
<form name="form" action="AddStaff.action" method="post" onsubmit="return checkform()">
<table width="25%" align="center">
<tbody>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>员工编号:<br/><br/></th>
<td><input type="text" name="staff.staffid"/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>姓名:<br/><br/></th>
<td><input type="text" name="staff.staffname"/></td></tr>
<tr>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Text</title>
</head>
<body>
<form>
<table>
<tr>
<th align="center" valign="middle" nowrap="nowrap">购买时间:</th>
<td align="left" valign="middle"><input name="equip.buytime" type="text" size="50"/></td>
<td valign="middle">2009-10-10</td>
</tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap">价格:</th>
<td align="left" valign="middle"><input name="equip.price" type="text" size="50"/></td>
<td valign="middle">*2000.00 保留小数点后两位</td>
</tr>
<tr>
<th>提交:</th>
<td>
<input id="OK" type="button" value="提交"/>
</td>
</tr>
</table>
</form>
<script>
document.getElementById("OK").onclick = function(){
var form = document.forms[0],
time = form["equip.buytime"].value,
price = form["equip.price"].value;
var r = time.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null){
alert("请以2000-10-10格式输入时间");
return false;
}
var re = /^(([1-9]\d*)|0)(\.\d{1,2})?$/
if(!re.test(price)){
alert("请以2000.00格式输入价格");
return false;
}
}
</script>
</body>
</html>
问题一为低级错误:
var time=window.document.form["equip.buytime"];应改为
var time=window.document.form["equip.buytime"].value;var price=window.document.form["equip.price"];应改为
var price=window.document.form["equip.price"].value;问题2可能是编辑器的问题,代码没有问题