JS簡單實現(xiàn)登陸驗證附效果圖
更新時間:2013年11月19日 16:21:43 作者:
實現(xiàn)登陸驗證的方法有很多,在本文為大家詳細介紹下使用js是如何做到的,下面有個不錯的示例還有運行截圖,喜歡的朋友可以嘗試操作下
源代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="劉江波">
<script type="text/javascript">
function login(){
var userName=document.getElementById("userName").value;
var pwd=document.getElementById("pwd").value;
var repwd=document.getElementById("repwd").value;
var address=document.getElementById("address").value;
var matchResult=true;
if(userName==""||pwd==""||repwd==""||address==""){
alert("請確認是否有空缺項!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應在6到20個字符之間!");
matchResult=false;
}else if(userName==pwd||userName==repwd){
alert("密碼或重復密碼不能和用戶名相同!");
matchResult=false;
}else if(pwd.length<6||pwd.length>20||repwd.length<6||repwd.length>20){
alert("密碼或重復密碼長度應在6到20個字符之間!");
matchResult=false;
}else if(pwd!=repwd){
alert("密碼和重復密碼不同,請重新輸入!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應在6到20個字符之間!");
matchResult=false;
}
if(matchResult==true){
var mailreg = /^\w+@\w+(\.\w+)+$/;
if(!address.match(mailreg)){
alert("郵箱格式不正確");
matchResult=false;
}
}
if(matchResult==true){
if(userName.charAt(0)>=0&&userName.charAt(0)<=9){
alert("用戶名不能以數(shù)字字符開始!");
matchResult=false;
}
}
return matchResult;
}
</script>
<title>用戶注冊及驗證</title>
</head>
<body>
<center>
<form name="loginForm" action="http://www.ytu.edu.cn" onsubmit="return login()" method="post">
<table bgcolor="#6666FF" width="300" cellspacing="0" cellpadding="0" border="0" align="left" valign="top">
<tr>
<td class="table-title" colspan="2" align="center" bgcolor="#3366FF">用戶注冊</td>
</tr>
<tr>
<td width="130" height="28" align="left">登錄用戶名</td>
<td><input id="userName" name="userName" type="text" class="input"></td>
</tr>
<tr>
<td width="80" height="28" align="left">登錄密碼</td>
<td><input id="pwd" name="pwd" type="password" class="input"></td>
</tr>
<tr>
<td width="80" height="28" align="left">重復輸入密碼</td>
<td><input id="repwd" name="repwd" type="password" class="input"></td>
</tr>
<tr>
<td width="80" height="28" align="left">有效郵箱地址</td>
<td><input id="address" name="address" type="text" class="input"></td>
</tr>
<tr>
<!--<td width="10" height="28" align="left"></td>-->
<td colspan="2">
<input type="submit" value="登錄">
<input type="button" value="取消" onClick="reset()"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
效果實現(xiàn):
復制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="劉江波">
<script type="text/javascript">
function login(){
var userName=document.getElementById("userName").value;
var pwd=document.getElementById("pwd").value;
var repwd=document.getElementById("repwd").value;
var address=document.getElementById("address").value;
var matchResult=true;
if(userName==""||pwd==""||repwd==""||address==""){
alert("請確認是否有空缺項!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應在6到20個字符之間!");
matchResult=false;
}else if(userName==pwd||userName==repwd){
alert("密碼或重復密碼不能和用戶名相同!");
matchResult=false;
}else if(pwd.length<6||pwd.length>20||repwd.length<6||repwd.length>20){
alert("密碼或重復密碼長度應在6到20個字符之間!");
matchResult=false;
}else if(pwd!=repwd){
alert("密碼和重復密碼不同,請重新輸入!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應在6到20個字符之間!");
matchResult=false;
}
if(matchResult==true){
var mailreg = /^\w+@\w+(\.\w+)+$/;
if(!address.match(mailreg)){
alert("郵箱格式不正確");
matchResult=false;
}
}
if(matchResult==true){
if(userName.charAt(0)>=0&&userName.charAt(0)<=9){
alert("用戶名不能以數(shù)字字符開始!");
matchResult=false;
}
}
return matchResult;
}
</script>
<title>用戶注冊及驗證</title>
</head>
<body>
<center>
<form name="loginForm" action="http://www.ytu.edu.cn" onsubmit="return login()" method="post">
<table bgcolor="#6666FF" width="300" cellspacing="0" cellpadding="0" border="0" align="left" valign="top">
<tr>
<td class="table-title" colspan="2" align="center" bgcolor="#3366FF">用戶注冊</td>
</tr>
<tr>
<td width="130" height="28" align="left">登錄用戶名</td>
<td><input id="userName" name="userName" type="text" class="input"></td>
</tr>
<tr>
<td width="80" height="28" align="left">登錄密碼</td>
<td><input id="pwd" name="pwd" type="password" class="input"></td>
</tr>
<tr>
<td width="80" height="28" align="left">重復輸入密碼</td>
<td><input id="repwd" name="repwd" type="password" class="input"></td>
</tr>
<tr>
<td width="80" height="28" align="left">有效郵箱地址</td>
<td><input id="address" name="address" type="text" class="input"></td>
</tr>
<tr>
<!--<td width="10" height="28" align="left"></td>-->
<td colspan="2">
<input type="submit" value="登錄">
<input type="button" value="取消" onClick="reset()"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
效果實現(xiàn):
您可能感興趣的文章:
相關文章
深入理解JavaScript中的對象復制(Object Clone)
下面小編就為大家?guī)硪黄钊肜斫釰avaScript中的對象復制(Object Clone)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
解決 firefox 不支持 document.all的方法
解決 firefox 不支持 document.all的方法...2007-03-03
javascript中數(shù)組的多種定義方法和常用函數(shù)簡介
本文簡單介紹了javascript一維數(shù)組和二維數(shù)組的定義方法集錦以及常用函數(shù)簡介。2014-05-05

