js验证用户注册
我现在用js验证用户注册· 代码太多。。。。那位大神给我修改下代码。。。
<html>
<head>
<title>用户注册</title>
<meta http-equiv="content-type" content="text/html; charset=gbk">
</head>
<script>
//验证用户
function checkname(str) {
if (str.length > 20 || str.length < 6) {
document.getElementById("usernameErr").innerHTML = "<fontcolor='red'>用户名必须大于3位小于20位</font> ";
form.name.focus();
}else{
document.getElementById("usernameErr").innerHTML = "<font>用户名必须大于3位小于20位</font> ";
}
}
//2次验证用户密码
function checkpassword2() {
var password1 = document.form.password1.value;
var password2 = document.form.password2.value;
if (password1 != password2) {
document.getElementById("password2Err").innerHTML = "<fontcolor='red'>俩次密码必须一样</font> ";
form.password2.focus();
}else{
document.getElementById("password2Err").innerHTML = "<font>俩次密码必须一样</font> ";
}
}
//动态调整头像
function checkface() {
var face = document.getElementById("face").value;
if (face != null) {
document.getElementById("faceimg").src = face;
}
}
//验证数字
function checkEmail(str){
if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
{
document.getElementById("emailErr").innerHTML = "<font color='red'>请输入正确的邮箱</font> ";
}else{
document.getElementById("emailErr").innerHTML = "<font>格式如jaksi@163.com</font> ";
}
}
//验证年龄
function checkAge(num)
{
for(var i=0;i<num.length;i++)
{
if(num.charAt(i)<'0' || num.charAt(i)>'9')
{
document.getElementById("ageErr").innerHTML = "<font color='red'>年龄大于0小于100岁</font> ";
}else{
document.getElementById("ageErr").innerHTML = "<font>年龄大于0小于100岁</font> ";
}
}
}
//验证手机号码
function checkphone(num){
for(var i=0;i<num.length;i++)
{
if(num.charAt(i)<'0' || num.charAt(i)>'9'||num.length!=11)
{
document.getElementById("phoneErr").innerHTML = "<font color='red'>请输入11位手机号码</font> ";
}else{
document.getElementById("phoneErr").innerHTML = "<font>11位手机号码:如13538089527</font> ";
}
}
}
//验证地址
function checkaddress(str)
{
if(str.length<2){
document.getElementById("addressErr").innerHTML = "<font color='red'>请输入地址</font> ";
}else{
document.getElementById("addressErr").innerHTML = "<font>如:中国中山</font> ";
}
}
//CharMode函数
//测试某个字符是属于哪一类.
function CharMode(iN) {
if (iN >= 48 && iN <= 57) //数字
return 1;
if (iN >= 65 && iN <= 90) //大写字母
return 2;
if (iN >= 97 && iN <= 122) //小写
return 4;
else
return 8; //特殊字符
}
//bitTotal函数
//计算出当前密码当中一共有多少种模式
function bitTotal(num) {
modes = 0;
for (i = 0; i < 4; i++) {
if (num & 1)
modes++;
num >>>= 1;
}
return modes;
}
//checkStrong函数
//返回密码的强度级别
function checkStrong(sPW) {
if (sPW.length < 6)
return 0; //密码太短
Modes = 0;
for (i = 0; i < sPW.length; i++) {
//测试每一个字符的类别并统计一共有多少种模式.
Modes |= CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
}
//pwStrength函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
function pwStrength(pwd) {
O_color = "#eeeeee";
L_color = "#FF0000";
M_color = "#FF9900";
H_color = "#33CC00";
if (pwd.length > 20 || pwd.length < 6) {
document.getElementById("password1Err").innerHTML = "<fontcolor='red'>用户名必须大于3位小于20位</font> ";
form.name.focus();
}else{
document.getElementById("password1Err").innerHTML = "<font>用户名必须大于3位小于20位</font> ";
}
if (pwd == null || pwd == '') {
Lcolor = Mcolor = Hcolor = O_color;
} else {
S_level = checkStrong(pwd);
switch (S_level) {
case 0:
Lcolor = Mcolor = Hcolor = O_color;
case 1:
Lcolor = L_color;
Mcolor = Hcolor = O_color;
break;
case 2:
Lcolor = Mcolor = M_color;
Hcolor = O_color;
break;
default:
Lcolor = Mcolor = Hcolor = H_color;
}
}
document.getElementById("strength_L").style.background = Lcolor;
document.getElementById("strength_M").style.background = Mcolor;
document.getElementById("strength_H").style.background = Hcolor;
return;
}
</script>
<body>
<center>
<h3>
用户注册
</h3>
<form action="chkregister.jsp" name="form">
<table width="70%" align="center" border="1">
<tr>
<td align="right">
用户名:
</td>
<td width="20%">
<input type="text" name="name"
onblur="checkname(this.value.toLowerCase())" />
</td>
<td id="usernameErr" width="30%">
用户名必须大于3位小于20位
</td>
</tr>
<tr>
<td align="right" width="20%">
密码:
</td>
<td width="20%">
<input type="text" name="password1"
onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value) />
</td>
<td width="30%" id="password1Err">密码必须大于6位小于20位</td>
</tr>
<tr align="center" bgcolor="#eeeeee">
<td width="10%" id="strength_L">
弱
</td>
<td width="10%" id="strength_M">
中
</td>
<td width="10%" id="strength_H">
强
</td>
</tr>
<tr>
<td align="right">
再次确认密码:
</td>
<td width="20%">
<input type="text" name="password2" onblur="checkpassword2()" />
</td>
<td id="password2Err"></td>
</tr>
<tr>
<td align="right">
电子邮箱:
</td>
<td width="20%">
<input type="text" name="email" onblur="checkEmail(this.value)" />
</td>
<td id="emailErr">格式如jaksi@163.com</td>
</tr>
<tr>
<td align="right">
性别:
</td>
<td width="20%">
<input type="radio" name="sex" value="男" checked="checked" />
男
<input type="radio" name="sex" value="女" />
女
</td>
</tr>
<tr>
<td align="right">
年龄:
</td>
<td width="20%">
<input type="text" name="age" onblur="checkAge(this.value)" />
</td>
<td id="ageErr">年龄大于0小于100岁</td>
</tr>
<tr>
<td align="right">
头像:
</td>
<td width="20%">
<select name="face" id="face" size=1 onblur="checkface()">
<option value=images/face/1.gif>
图像1
</option>
<option value=images/face/2.gif>
图像2
</option>
<option value=images/face/3.gif>
图像3
</option>
<option value=images/face/4.gif>
图像4
</option>
<option value=images/face/5.gif>
图像5
</option>
<option value=images/face/6.gif>
图像6
</option>
<option value=images/face/7.gif>
图像7
</option>
<option value=images/face/8.gif>
图像8
</option>
<option value=images/face/9.gif>
图像9
</option>
<option value=images/face/10.gif>
图像10
</option>
<option value=images/face/11.gif>
图像11
</option>
<option value=images/face/12.gif>
图像12
</option>
<option value=images/face/13.gif>
图像13
</option>
</select>
<img id="faceimg" src="images/face/1.gif" border="0"
width="120px" height="120px" />
</td>
</tr>
<tr>
<td align="right">
联系电话:
</td>
<td width="20%">
<input type="text" name="phone" onblur="checkphone(this.value)"/>
</td>
<td id="phoneErr">如:13835928542</td>
</tr>
<tr>
<td align="right">
地址:
</td>
<td width="20%">
<input type="text" name="address" onblur="checkaddress(this.value)" />
</td>
<td id="addressErr">如:火星</td>
</tr>
</table>
<input type="submit" value="注册" />
<input type="reset" value="重置" />
</form>
</center>
</body>
</html>
[解决办法]
给你个提示吧。你每个方法都设成 boolean类型的。 如果通过了就返回ture 否则false! 最后一起判断通过就提交。否则。。。。。
- JScript code
function a(){if(1==1){return true;}else{return false;} } function check(){if(a()){提交}}