<script language="javascript">
function check(){
  if(document.getElementById('username').value==""){
alert("用戶名不能為空");
return false;
}if(....){
  //判斷密碼;(省略了)
}
return ture;}
</script>
为什么当我输入用户名后,就不再判断密码了呢? 
我想在这一个函数function check()中判断用户名和密码,
要么哪位大哥,帮我写一个完整的代码了,

解决方案 »

  1.   

    判断用户名返回了下面没执行    return false;  
      

  2.   

    呃 应该是这样 如果你的判断密码也是判断为空之类的代码。当2个都不为空。。返回了true
      

  3.   

    function validatePwd() {                                     //验证两次输入的密码是否一致,并且至少6位
    if(document.getElementById("department1").value== ''){
    alert('Please select department!');
    return false;
    }
    if(document.getElementById("department2").value== ''){
    alert('Please select department!');
    return false;
    }

    var invalid = " "; // Invalid character is a space
    var minLength = 6; // Minimum length
    var pw1 = document.getElementById("pw").value;
    var pw2 = document.getElementById("cp").value;
    // check for a value in both fields.
    if (pw1 == '' || pw2 == '') {
    alert('Please enter your password twice.');
    return false;
    }
    // check for minimum length
    if (document.getElementById("pw").value.length < minLength) {
    alert('Your password must be at least ' + minLength + ' characters long. Try again.');
    return false;
    }
    // check for spaces
    if (document.getElementById("pw").value.indexOf(invalid) > -1) {
    alert("Sorry, spaces are not allowed.");
    return false;
    }
    else {
    if (pw1 != pw2) {
    alert ("You did not enter the same new password twice. Please re-enter your password.");
    return false;
    }
    else {
    alert('Please nice job !');
    return true;
          }
       }}
      

  4.   

    不是吧?!我把你上面的代码里的检验两次密码输入的那个测试了,没有问题啊!(我把前两个注释了,只测试了2次密码的那个)<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>两次密码输入验证</title>
    <script language="javascript"> 
    function validatePwd() {                                     //验证两次输入的密码是否一致,并且至少6位
    //    if(document.getElementById("department1").value== ''){
    //        alert('Please select department!');
    //        return false;
    //    }
    //    if(document.getElementById("department2").value== ''){
    //        alert('Please select department!');
    //        return false;
    //    }    
        
    var invalid = " "; // Invalid character is a space
    var minLength = 6; // Minimum length
    var pw1 = document.getElementById("pw").value;
    var pw2 = document.getElementById("cp").value;
    // check for a value in both fields.
    if (pw1 == '' || pw2 == '') {
    alert('Please enter your password twice.');
    return false;
    }
    // check for minimum length
    if (document.getElementById("pw").value.length < minLength) {
    alert('Your password must be at least ' + minLength + ' characters long. Try again.');
    return false;
    }
    // check for spaces
    if (document.getElementById("pw").value.indexOf(invalid) > -1) {
    alert("Sorry, spaces are not allowed.");
    return false;
    }
    else {
    if (pw1 != pw2) {
    alert ("You did not enter the same new password twice. Please re-enter your password.");
    return false;
    }
    else {
    alert('Please nice job !');
    return true;
          }
       }}
    </script>
    <style>
    body{
    text-align:center;
    }
    #container{
    width:450px;
    height:300px;
    border:1px solid green;
    text-align:center;
    }
    </style></head><body>
    <div id="container">
    <form>
    <input type="text" id="pw"/>
    <input type="text" id="cp"/>
    <input type="button" onclick="validatePwd()" value="校验"/>
    </form>
    </div></body>
    </html>
      

  5.   

    密码是没问题,但和两个 if(document.getElementById("department1").value== ''){
     放一起就有问题了, 放一起时,就是会判断 前面两个if() 了,后面的密码就不会判断了(也就是不输密码也不会提示了),  我发贴 的主要目的也就是要解决这个问题. 谢谢了!
      

  6.   

    你的html代码贴出来看看。 也可能是你的html代码问题
      

  7.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <script>
    function checks()
    {
      if(document.getElementById("username").value=="")
      {
        alert("user error");
    return false;
      }
      if(document.getElementById("password").value=="")
      {
        alert("pwd error");
    return false;
      }
      return true;
    }
      </script>
     </HEAD> <BODY>
      <input type="text" name="username" />
      <br/>
      <input type="password" name="password" />
      <br/>
      <input type="button" onclick="return checks();" name="on_submit" value="測試下載" />
     </BODY>
    </HTML>