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

解决方案 »

  1.   

    简单的做吧<tr>
    <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>
      

  2.   

    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>
    错了点
      

  3.   

    因为你没有包含在onload等方法里。
    所以var user那段要放在函数体内
      

  4.   

    这样做不行 就是因为 name="user.username" 不能改 我才发帖求助的
    var user重先声明这个对象 我在后台获取不到user了
      

  5.   

    var 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>这样做有异议了吗
      

  6.   

    <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>
      

  7.   


    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;
      }
      
      

  8.   

    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
      

  9.   

    document.forms[0][password].value.length==0 可以,把password换成变量就行了
      

  10.   

    顶6#
    只不过把useruser.name改为user.username就可以了
    低级错误
      

  11.   

    完全按照六楼的做法 也是验证不了 
    但是在我原有的基础上加上六楼关于类似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>只改这一段 其他不动
      

  12.   


    又发现自己一个低级错误 我在window.document..form["user.password"].value.length==0
    document后面多加了一个.  改掉之后都好使了