JS簡單實(shí)現(xiàn)登陸驗(yàn)證附效果圖
更新時間:2013年11月19日 16:21:43 作者:
實(shí)現(xiàn)登陸驗(yàn)證的方法有很多,在本文為大家詳細(xì)介紹下使用js是如何做到的,下面有個不錯的示例還有運(yù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("請確認(rèn)是否有空缺項(xiàng)!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應(yīng)在6到20個字符之間!");
matchResult=false;
}else if(userName==pwd||userName==repwd){
alert("密碼或重復(fù)密碼不能和用戶名相同!");
matchResult=false;
}else if(pwd.length<6||pwd.length>20||repwd.length<6||repwd.length>20){
alert("密碼或重復(fù)密碼長度應(yīng)在6到20個字符之間!");
matchResult=false;
}else if(pwd!=repwd){
alert("密碼和重復(fù)密碼不同,請重新輸入!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應(yīng)在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>用戶注冊及驗(yàn)證</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">重復(fù)輸入密碼</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>
效果實(shí)現(xiàn):
復(fù)制代碼 代碼如下:
<!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("請確認(rèn)是否有空缺項(xiàng)!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應(yīng)在6到20個字符之間!");
matchResult=false;
}else if(userName==pwd||userName==repwd){
alert("密碼或重復(fù)密碼不能和用戶名相同!");
matchResult=false;
}else if(pwd.length<6||pwd.length>20||repwd.length<6||repwd.length>20){
alert("密碼或重復(fù)密碼長度應(yīng)在6到20個字符之間!");
matchResult=false;
}else if(pwd!=repwd){
alert("密碼和重復(fù)密碼不同,請重新輸入!");
matchResult=false;
}else if(userName.length<6||userName.length>20){
alert("用戶名長度應(yīng)在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>用戶注冊及驗(yàn)證</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">重復(fù)輸入密碼</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>
效果實(shí)現(xiàn):

您可能感興趣的文章:
- jquery實(shí)現(xiàn)界面無刷新加載登陸注冊
- struts2+jquery實(shí)現(xiàn)ajax登陸實(shí)例詳解
- jQuery的cookie插件實(shí)現(xiàn)保存用戶登陸信息
- 使用jQuery插件創(chuàng)建常規(guī)模態(tài)窗口登陸效果
- jQuery登陸判斷簡單實(shí)現(xiàn)代碼
- 將jQuery應(yīng)用于login頁面的問題及解決
- Jsp中解決session過期跳轉(zhuǎn)到登陸頁面并跳出iframe框架的方法
- httpclient模擬登陸具體實(shí)現(xiàn)(使用js設(shè)置cookie)
- ExtJs 表單提交登陸實(shí)現(xiàn)代碼
- jQuery+Pdo編寫login登陸界面
相關(guān)文章
JavaScript基礎(chǔ)知識之?dāng)?shù)據(jù)類型
JavaScript中有5種簡單數(shù)據(jù)類型(也稱為基本數(shù)據(jù)類型):Undefined、Null、Boolean、Number和String。還有1種復(fù)雜數(shù)據(jù)類型——Object,Object本質(zhì)上是由一組無序的名值對組成的2012-08-08原生js實(shí)現(xiàn)節(jié)日時間倒計時功能
本文主要分享了原生js實(shí)現(xiàn)節(jié)日時間倒計時功能的示例代碼。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01深入理解JavaScript中的對象復(fù)制(Object Clone)
下面小編就為大家?guī)硪黄钊肜斫釰avaScript中的對象復(fù)制(Object Clone)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05解決 firefox 不支持 document.all的方法
解決 firefox 不支持 document.all的方法...2007-03-03javascript中數(shù)組的多種定義方法和常用函數(shù)簡介
本文簡單介紹了javascript一維數(shù)組和二維數(shù)組的定義方法集錦以及常用函數(shù)簡介。2014-05-05JavaScript 遞增、遞減運(yùn)算符實(shí)例
遞增、遞減運(yùn)算符實(shí)例,基礎(chǔ)Js代碼范例,新手可參考哦。2010-07-07