function checkform()
{
if(window.document.form.{user.username}.value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(document..form.{user.password}.value.length==0)
{
alert("密码不能为空");
return false;
}
}</script>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>用户账号:<br/><br/></th>
<td><input type="text" name="user.username"/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>密码:<br/><br/></th>
<td><input type="password" name="user.password"/></td></tr>
<tr>
这个为什么会判断不了?
如果把上面的加粗的换成username和password可以判断输入是否为空
望高手指点迷津
{
if(window.document.form.{user.username}.value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(document..form.{user.password}.value.length==0)
{
alert("密码不能为空");
return false;
}
}</script>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>用户账号:<br/><br/></th>
<td><input type="text" name="user.username"/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>密码:<br/><br/></th>
<td><input type="password" name="user.password"/></td></tr>
<tr>
这个为什么会判断不了?
如果把上面的加粗的换成username和password可以判断输入是否为空
望高手指点迷津
<th align="center" valign="middle" nowrap="nowrap"><br/>用户账号:<br/><br/></th>
<td><input type="text" name="username"/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>密码:<br/><br/></th>
<td><input type="password" name="password"/></td></tr>
<tr>var user = {
username = document.getElementsByName("username")[0],
password = document.getElementsByName("password")[0]
}
function checkform()
{
if(user.username.value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(user.password.value.length==0)
{
alert("密码不能为空");
return false;
}
}</script>
username : document.getElementsByName("username")[0],
password : document.getElementsByName("password")[0]
}
function checkform()
{
if(user.username.value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(user.password.value.length==0)
{
alert("密码不能为空");
return false;
}
}</script>
错了点
所以var user那段要放在函数体内
var user重先声明这个对象 我在后台获取不到user了
username : document.getElementsByName("user.username")[0],
password : document.getElementsByName("user.password")[0]
}
function checkform()
{
if(user.username.value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(user.password.value.length==0)
{
alert("密码不能为空");
return false;
}
}</script>这样做有异议了吗
function checkform()
{
if(document.forms[0]["useruser.name"].value.length ===0 )
{
alert("用户账号不能为空");
return false;
}
if(document.forms[0]["user.password"].value.length==0)
{
alert("密码不能为空");
return false;
} }
</script>
<form name="fm" action="">
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>用户账号:<br/><br/></th>
<td><input type="text" name="user.username" onblur="checkform();"/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap" ><br/>密码:<br/><br/></th>
<td><input type="password" name="user.password" onblur="checkform();"/></td></tr>
<tr>
</form>
function checkform()
{var user = {
username : document.getElementsByName("user.username")[0],
password : document.getElementsByName("user.password")[0]
};
if(user.username.value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(user.password.value.length==0)
{
alert("密码不能为空");
return false;
}
{
if(document.form.username.value=="")
{
alert("用户账号不能为空");
return false;
}
if(document.form.password.value=="")
{
alert("密码不能为空");
return false;
}
}</script>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>用户账号:<br/><br/></th>
<td><input type="text" name="username" value=""/></td></tr>
<tr>
<th align="center" valign="middle" nowrap="nowrap"><br/>密码:<br/><br/></th>
<td><input type="password" name="password" value=""/></td></tr>
<tr>
//主要是你type中得value得值没有设置为空。如果没写获取得长度就不是为0
只不过把useruser.name改为user.username就可以了
低级错误
但是在我原有的基础上加上六楼关于类似forms[0]["useruser.name"]这一段
可以在火狐中完成username的验证 password为空照样验证不了
而在IE6中两者都不能验证
代码如下
<script type="text/javascript">
function checkform()
{
if(window.document.form["user.username"].value.length==0)
{
alert("用户账号不能为空");
return false;
}
if(window.document..form["user.password"].value.length==0)
{
alert("密码不能为空");
return false;
}
}</script>只改这一段 其他不动
又发现自己一个低级错误 我在window.document..form["user.password"].value.length==0
document后面多加了一个. 改掉之后都好使了