自己写的js验证例子,不满足条件时会有文字提示的!不用alert!

来源:互联网 发布:windows安装nginx 编辑:程序博客网 时间:2024/06/08 00:31

 

 <script type="text/javascript">
function check(obj){
if(obj.value.length < 6 || obj.value.length > 16) {

document.getElementById("errorinfo").style.display = "block";
document.getElementById("submit").disabled = true;
}
 
 
 
  else {
 
  document.getElementById("errorinfo").style.display = "none";
  document.getElementById("submit").disabled = false; 
  }
 
 
   
}
function checkPwd(obj)
{
  if (obj.value.length < 6 || obj.value.length > 16) {
  document.getElementById("errorpwd").style.display = "block";
  document.getElementById("submit").disabled = true;
 
 
 
  }
  else {
 
  document.getElementById("errorpwd").style.display = "none";
  document.getElementById("submit").disabled = false;
 
  }
 
   
}
function checkRePwd(obj)
{
  var p=document.getElementById("password");
  if(obj.value.length<6||obj.value.length>16)
  {
     document.getElementById("errorrepwd1").style.display = "block";
      document.getElementById("submit").disabled = true;
    
   

  }
  if (p.value != obj.value) {
  document.getElementById("errorrepwd2").style.display = "block";
  document.getElementById("submit").disabled = true;
 
 
 
  }
  else {
    document.getElementById("errorrepwd1").style.display = "none";
  document.getElementById("errorrepwd2").style.display = "none";
  document.getElementById("submit").disabled = false;
 
  }
 
   
}
function checkemail(obj)
{
var cEmail=obj.value;

if(cEmail.match(/[\w-]+@{1}[\w-]+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig)!=cEmail){
  document.getElementById("erroremail").style.display = "block";
   document.getElementById("submit").disabled = true;
 
  }
else{
  document.getElementById("erroremail").style.display = "none";
   document.getElementById("submit").disabled = false;
 
  }


 
   
}
function checkqq(obj){
var q=obj.value;
var filter=/^\s*[.0-9]{5,10}\s*$/;
    if(!filter.test(q)){
    document.getElementById("errorqq").style.display = "block";
    }else{
    document.getElementById("errorqq").style.display = "none";
   

}
}


</script>






<link href="<%=request.getContextPath()%>/style.css" rel="stylesheet" type="text/css">
</head>

<body bgcolor="#CCCCCC"  >
<p style="color:#FF0000" align="center">温馨提示:带&quot;*&quot;为必填内容</p>

<form name="form1" method="post" action="servlet/RegisterServlet" >
  <table width="41%" height="300" border="0" align="center" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
    <tr bgcolor="#FFFFFF">
      <td height="27" colspan="2" align="center" background="images/table.gif"><strong><font color="#FFFFFF">欢迎加入</font></strong></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td width="19%" height="27" align="center">账号:</td>
      <td width="81%" height="27"><input name="username" type="text" id="username" onblur="check(this)"/>
     * <div style="display:none" id="errorinfo">账号长度必须在6至16个字符之间</div></td>
    
    </tr>
    <tr bgcolor="#FFFFFF">
      <td width="19%" height="27" align="center">密码:</td>
      <td width="81%" height="27"><input name="password" type="password" id="password" onblur="checkPwd(this)"/>
     * <div style="display:none" id="errorpwd">密码长度必须在6至16个字符之间</div></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td width="19%" height="27" align="center">再次输入密码:</td>
      <td width="81%" height="27"><input name="rePassword" type="password" id="rePassword" onblur="checkRePwd(this)" />
     *
      <div style="display:none" id="errorrepwd1">两次输入密码不一致</div>
      <div style="display:none" id="errorrepwd2">密码长度必须在6至16个字符之间</div></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td height="27" align="center">性别:</td>
      <td height="27"><select name="sex" id="sex">
          <option value="男" selected>男</option>
          <option value="女">女</option>
        </select></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td height="27" align="center">邮件:</td>
      <td height="27"><input name="email" type="text" id="email" onblur="checkemail(this)"/>*
<div style="display:none" id="erroremail">输入邮箱格式有误</div></td>

    </tr>
  
    <tr bgcolor="#FFFFFF">
      <td height="31" align="center">QQ:</td>
      <td height="31"><input name="qq" type="text" id="qq" onblur="checkqq(this)" />
    

      <div style="display:none" id="errorqq">请输入正确的qq</div></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td height="31" align="center">头象:</td>
      <td height="31"><input name="tx" type="radio" value="images/face01.gif" checked>
        <img src="<%=basePath%>images/face01.gif" width="32" height="32">
        <input type="radio" name="tx" value="images/face02.gif">
        <img src="<%=basePath%>images/face02.gif" width="32" height="32">
        <input type="radio" name="tx" value="images/face03.gif">
        <img src="<%=basePath%>images/face03.gif" width="32" height="32"> <br>
        <input type="radio" name="tx" value="images/face04.gif">
        <img src="<%=basePath%>images/face04.gif" width="32" height="32">
        <input type="radio" name="tx" value="images/face05.gif">
        <img src="<%=basePath%>images/face05.gif" width="32" height="32">
        <input type="radio" name="tx" value="images/face06.gif">
        <img src="<%=basePath%>images/face06.gif" width="32" height="32"></td>
    </tr>
   
    <tr bgcolor="#FFFFFF">
   
      <td height="31" colspan="2" align="center">
       <input type="submit" value="提交"  id="submit" style="width:50px" disabled="disabled"/>    &nbsp;&nbsp;
  <input style="width:50px;" type="reset" value="重置"/>&nbsp;&nbsp;
  <input style="width:50px;" type="button" value="返回" onClick="window.location.href='http://localhost:8080/lastuse/servlet/QueryServlet'">
      </td>
    </tr>
  </table>
</form>

原创粉丝点击