<script language="javascript" type="text/javascript"/>
function checkid(){
var id=document.form1.xuehao.value
if(id==""){
document.getElementById("id").innerHTML="<font color='red' size='-1'>× 请输入学号</font>";
return false;
}
if(id.length!=6){
document.getElementById("id").innerHTML="<font color='red' size='-1'>× 学号必须为6位</font>";
return false;

}else{
document.getElementById("id").innerHTML="<font color='blue' size='-1'>√ 正确</font>";
}
}
 
function chename(){
var yonghuming=document.form1.username.value
if(id=""){
document.getElementById("yonghuming").innerHTML="<font color='red' size='-1'>用户名不能为空</font>";
return false;
            }


}
</script><form action="" method="post" name="form1" alingn="left">用户名:<input  type="text" name="username" onblur="chename()" /><div id="yonghuming">(*用户名必须是字母)<br/>
学 号:<input type="text" name="xuehao" onblur="checkid()"/><div id="id"></div>(*学号必须为6位)<br/>
邮  箱:<input type="text" name="youxiang"/><div id="mail">(*必须包含@和.)<br/><input type="submit"  value="提交" name="tijiao" />
<input  type="reset" value="重置" name="reset"  />
  
  </form>
</body>
</html>用户名验证的出不来!

解决方案 »

  1.   

    除了 #1 说的问题,还有变量名写错了
    var yonghuming=document.form1.username.value
                if(yonghuming==""){
      

  2.   

    <script language="javascript" type="text/javascript"/>
    function checkid(){
    var id=document.form1.xuehao.value
    if(id==""){
    document.getElementById("id").innerHTML="<font color='red' size='-1'>× 请输入学号</font>";
    return false;
    }
    if(id.length!=6){
    document.getElementById("id").innerHTML="<font color='red' size='-1'>× 学号必须为6位</font>";
    return false;
     
    }else{
    document.getElementById("id").innerHTML="<font color='blue' size='-1'>√ 正确</font>";
    }
    return true;
    }
          
    function chename(){
    var yonghuming=document.form1.username.value
    if(yonghuming ==""){
    document.getElementById("yonghuming").innerHTML="<font color='red' size='-1'>用户名不能为空</font>";
    return false;
    }   
    return true;
    }function check_all(){
    var flag = true;
    flag = checkid();
    if (!flag){
    return false;
    } flag = chename();
    if (!flag){
    return false;
    }
    document.form1.submit();
    }
    </script>
     
    <form action="" method="post" name="form1" alingn="left">
     
    用户名:<input  type="text" name="username" onblur="chename()" /><div id="yonghuming"></div>(*用户名必须是字母)<br/>
    学 号:<input type="text" name="xuehao" onblur="checkid()"/><div id="id"></div>(*学号必须为6位)<br/>
    邮  箱:<input type="text" name="youxiang"/><div id="mail">(*必须包含@和.)<br/>
    <input type="button"  value="提交" name="tijiao" onclick="check_all()"/>
    <input  type="reset" value="重置" name="reset"  />
    </form>
    </body>
    </html>