Javascript验证注册的时候,用户名不能为空,两次密码不一致的代码该怎么写啊!请高手帮忙.....
1个回答
展开全部
多看看书吧,这个很简单,你看都没人回。
我给你一个吧。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!--
function fnValData(){
var uname = document.getElementById("uname").value;
var upass = document.getElementById("upass").value;
var rpass = document.getElementById("rpass").value;
var regex = /^\w{5,12}$/i;
if(!regex.test(uname)){
alert("username must between 5 and 12 charaters.");
return false;
}else if(!regex.test(upass)){
alert("password must between 5 and 12 charaters.");
return false;
}else if(!regex.test(rpass) || !rpass == upass){
alert("Re-Password must be equals with password.");
return false;
}
}
//-->
</script>
</head>
<body>
<form method="post" action="#" onsubmit="return fnValData();">
Username:<input type="text" id="uname" name="username"/></br>
Password:<input type="text" id="upass" name="password"/></br>
Re-Password:<input type="text" id="rpass" name="password2"/></br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
我给你一个吧。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!--
function fnValData(){
var uname = document.getElementById("uname").value;
var upass = document.getElementById("upass").value;
var rpass = document.getElementById("rpass").value;
var regex = /^\w{5,12}$/i;
if(!regex.test(uname)){
alert("username must between 5 and 12 charaters.");
return false;
}else if(!regex.test(upass)){
alert("password must between 5 and 12 charaters.");
return false;
}else if(!regex.test(rpass) || !rpass == upass){
alert("Re-Password must be equals with password.");
return false;
}
}
//-->
</script>
</head>
<body>
<form method="post" action="#" onsubmit="return fnValData();">
Username:<input type="text" id="uname" name="username"/></br>
Password:<input type="text" id="upass" name="password"/></br>
Re-Password:<input type="text" id="rpass" name="password2"/></br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
更多追问追答
追问
var reg1 = /^\w{5,12}$/i;
if (!reg1.test(name)) {
alert("用户名必须为5-15位!!!");
return false;
}
这个那为什么我输了6位,还是弹出了对话框呢?
追答
首先确定你的name是不是有值,是什么值?
\w代表[a-z0-9]的集合,只能是这些值。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询