首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JS验证输入不为空有关问题

2012-02-08 
JS验证输入不为空问题function checkform(){if(window.document.form.{user.username}.value.length0){a

JS验证输入不为空问题
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可以判断输入是否为空
望高手指点迷津

[解决办法]
<script type="text/javascript">
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>
[解决办法]

JScript code
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;  }
[解决办法]
探讨
<script type="text/javascript">
function checkform()
{
if(document.forms[0]["useruser.name"].value.length ===0 )
{
alert("用户账号不能为空");
return false;
}
if(document.forms[0]["user.p……

[解决办法]
function checkform()
{
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
[解决办法]
document.forms[0][password].value.length==0 可以,把password换成变量就行了

热点排行