写了一个无比简单的注册网页和一个js文件,为什么js验证注册只做第一个条件的验证,后面的(密码、邮箱)什么的都不做验证呢?错在哪里,怎样修改,麻烦大家教教小弟
代码在此:
Register.html
<!DOCTYPE html>
<html>
<head>
<title>Register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--导入CSS文件:register.css -->
<link rel="stylesheet" type="text/css" href="css/register.css">
<!--导入js文件:register.js -->
<script type="text/javascript" src="js/register.js" charset="UTF-8"></script>
</head>
<body>
<h1 align="center" id="tt">用户注册</h1>
<form method="post" name="form1" onSubmit="return check();">
<table align="center" border="0" valign="middle">
<tr>
<td id="t">用 户 名: </td>
<td><input type="text" name="username">
<td id="i">*由字母开头,后跟字母、数字或下划线</td>
</tr>
<tr>
<td id="t">密 码: </td>
<td><input type="password" name="password" >
<td id="i">*登录密码,至少6位</td>
</tr>
<tr>
<td >确认密码: </td>
<td><input type="password" name="repassword" >
<td id="i">*再次输入登录密码</td>
</tr>
<tr>
<td class="n">邮 箱: </td>
<td colspan="2"><input type="email" name="email" >
</td>
</tr>
<tr>
<td>性 别: </td>
<td colspan="2"><input type="radio" checked="checked" name="sex" value="男" >男
<input type="radio" name="sex" >女</td>
</tr>
<tr>
<td>籍 贯: </td>
<td colspan="2">
<select>
<option value="广东">广东</option>
<option value="上海">上海</option>
<option value="台湾">台湾</option>
</select>
</td>
</tr>
<tr>
<td>爱 好: </td>
<td colspan="2">
<input type="checkbox" name="hobby" value="羽毛球">羽毛球
<input type="checkbox" name="hobby" value="足球">足球
<input type="checkbox" name="hobby" value="台球">台球
</td>
</tr>
<tr>
<td>验 证 码: </td>
<td><input type="text" name="identifycode">
<td id="i">*必填</td>
</tr>
<tr align="center">
<td colspan="3">
<input type="submit" name="submit" value="注册" >
<input type="reset" name="reset" id="reset" value="重置">
</tr>
</table>
</form>
</body>
</html>
register.js:
function check(){
var name=document.form1.username.value;
var pwd=document.form1.password.value;
var repwd=document.form1.repassword.value;
var email=document.form1.email.value;
var hobby=document.form1.hobby.checked;
var regl=/[a-zA-Z]\w*/;
var reg2=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
if(name.length<=0){
alert("用户名不能为空!");
return false;
}
if(!reg1.test(name)){
alert("用户名格式不正确!");
return false;
}
if(pwd<6) {
alert("密码不能为空!");
return false;
}
if(pwd!=repwd){
alert("两次密码不一致!");
return false;
}
if(!reg2.test(email)){
alert("邮箱格式不正确!");
return false;
}
if(hobby==false){
alert("爱好不能为空!");
return false;
}
return true;
// document.form1.submit();
}
------解决方案--------------------
var regl=/[a-zA-Z]\w*/;
var reg2=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
reg1写错了吧,写成regl了