您的位置:首页 > Web前端 > JavaScript

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

2011-05-14 12:23 141 查看

<script type="text/javascript">


function check(obj){
if(obj.value.length < 6 || obj.value.length > 16) {

document.getElementById("errorinfo").style.display = "block";
[color=red] document.getElementById("submit").disabled = true;[/color]
}



else {

document.getElementById("errorinfo").style.display = "none";
[color=red] document.getElementById("submit").disabled = false;[/color]
}



}
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" 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">
[color=red]<input type="submit" value="提交" id="submit" style="width:50px" disabled="disabled"/>[/color]   
<input style="width:50px;" type="reset" value="重置"/>  
<input style="width:50px;" type="button" value="返回" onClick="window.location.href='http://localhost:8080/lastuse/servlet/QueryServlet'">
</td>
</tr>
</table>
</form>
:evil: 效果图如下 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: