good_good_study 发表于 2007-12-12 17:38

我想让document.write("不能为空!")代替“请输入由数字和字母组成的用户”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
td {
        font-size: 12px;
}
-->
</style>
<script language="javascript">
function checkusername(){
      var a=document.form1.userName.value
      if(a==""){
              document.write("不能为空!")
          }
      else{
          if(a.length<4){
           alert("长度小4!")  
           }
      }
     return true;
}

</script>
</head>

<body>
<form action="http://www.baidu.com" name="form1" target="_blank" id="form1" >
  <table width="498" height="91" border="1" align="center">
    <tr>
      <td height="26" align="right">用户名:</td>
      <td >
        <input type="text" name="username" id="userName" onblur="checkusername()"/><a>请输入由数字和字母组成的用户</a></td>
    </tr>
    <tr>
      <td height="26" align="right">密码:</td>
      <td height="26">
        <input type="text" name="textfield2" id="textfield2" />密码长度不能少于6位</td>
    </tr>
    <tr>
      <td height="26" align="right">重复密码:</td>
      <td >
        <input type="text" name="textfield3" id="textfield3" />再次输入密码</td>
    </tr>

  </table>
  <label>
  <div align="center">
    <input type="submit" name="button" id="button" value="提交"  />
    <input type="submit" name="button2" id="button2" value="提交" />
  </div>
  </label>
</form>
</body>
</html>

我现在做的是一个验证表单。我想做一个当你输入不情况后面会自动出现不同提示。


就象我输入错误后“请输入由数字和字母组成的用户
”就变成了时先定义好的字符串

渚薰 发表于 2007-12-13 08:40

<td >
        <input type="text" name="username" id="userName" onblur="checkusername()"/><span id="for_userName">请输入由数字和字母组成的用户</span>
</td>

document.getElementById('for_userName').innerHTML='不能为空';

页: [1]

编程论坛