卖火柴的老特工 发表于 2013-2-7 23:09:50

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

http://dl.iteye.com/upload/attachment/481219/f3de0bc7-4b35-3485-aba9-957944736fcb.png
<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">温馨提示:带"*"为必填内容</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" />
   * <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" />
   *<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"/>
   *
      <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" />*
<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"/>
   

      <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"/>     
<input style="width:50px;" type="reset" value="重置"/>  
<input style="width:50px;" type="button" value="返回" >
      </td>
    </tr>
</table>
</form>
http://www.agoit.com/images/smiles/icon_evil.gif 效果图如下
页: [1]
查看完整版本: 自己写的js验证例子,不满足条件时会有文字提示的!不用alert!