JS+HTML5本地存儲(chǔ)Localstorage實(shí)現(xiàn)注冊(cè)登錄及驗(yàn)證功能示例
本文實(shí)例講述了JS+HTML5本地存儲(chǔ)Localstorage實(shí)現(xiàn)注冊(cè)登錄及驗(yàn)證功能。分享給大家供大家參考,具體如下:
源碼引用的js、jquery都是在線的,代碼拷到本地就能運(yùn)行
登錄:
<html ng-app="ionicApp"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>登錄</title> <link rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="https://cdn.bootcss.com/ionic/1.3.2/js/ionic.bundle.min.js"></script> </head> <body> <div class="bar bar-header"> <div class="h1 title">登錄</div> </div> <div class="content has-header"> <div class="list"> <label class="item item-input"> <span class="input-label">用戶名:</span> <input id="loginName" type="text"> </label> <label class="item item-input"> <span class="input-label">密碼:</span> <input id="loginPsd" type="password"> </label> </div> <div class="padding"> <button οnclick="login()" class="button button-block button-positive">登錄</button> </div> </div> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> <script>function login(){if(NoKong()){if(localStorage.user){arr = eval(localStorage.user);//獲取localStoragevar k = 0;for(e in arr){if($('#loginName').val()==arr[e].loginName){if($('#loginPsd').val()==arr[e].loginPsd){alert('登錄成功');clear();k = 0;return;}else{alert('密碼錯(cuò)誤');clear();k = 0;return;}}else{ k = 1;}}if(k==1){alert('用戶名不存在');clear();}}else{alert('用戶名不存在');clear();}}}function clear(){$('#loginName').val('');$("#loginPsd").val('');}function NoKong(){if($('#loginName').val()==""){alert('用戶名不能為空');return false;}else if($('#loginPsd').val()==""){alert('密碼不能為空');return false;}return true;}</script> </body></html>
注冊(cè):
<html ng-app="ionicApp"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>注冊(cè)</title> <link rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="https://cdn.bootcss.com/ionic/1.3.2/js/ionic.bundle.min.js"></script> </head> <body> <div class="bar bar-header"> <div class="h1 title">注冊(cè)</div> </div> <div class="content has-header"> <div class="list"> <label class="item item-input"> <span class="input-label">用戶名:</span> <input id="loginName" type="text"> </label> <label class="item item-input"> <span class="input-label">密碼:</span> <input id="loginPsd" type="password"> </label> </div> <div class="padding"> <button οnclick="ZhuCe()" class="button button-block button-positive">注冊(cè)</button> </div> </div> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> <script> function ZhuCe(){ if(NoKong()){ var arr = []; if(localStorage.user){ arr = eval(localStorage.user); for(e in arr){ if($('#loginName').val()==arr[e].loginName){ alert('該賬號(hào)已被注冊(cè)'); clear(); return; } } } var user = {'loginName':$("#loginName").val(),'loginPsd':$("#loginPsd").val()}; arr.push(user); localStorage.user=JSON.stringify(arr); alert('注冊(cè)成功'); clear(); } } function clear(){ $('#loginName').val(''); $("#loginPsd").val(''); } function NoKong(){ if($('#loginName').val()==""){ alert('用戶名不能為空'); return false; }else if($('#loginPsd').val()==""){ alert('密碼不能為空'); return false; } return true; } </script> </body> </html>
更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《JavaScript+HTML5特效與技巧匯總》、《JavaScript圖形繪制技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
理解 JavaScript Scoping & Hoisting(二)
這篇文章主要介紹了理解 JavaScript Scoping & Hoisting,盡管對(duì)于有經(jīng)驗(yàn)的程序員來說這只是小菜一碟,不過我還是順著初學(xué)者常見的思路做一番描述2015-11-11JavaScript中的事件循環(huán)機(jī)制及其運(yùn)行原理
JavaScript中的事件循環(huán)機(jī)制是一種異步處理機(jī)制,通過維護(hù)事件隊(duì)列和消息隊(duì)列,實(shí)現(xiàn)任務(wù)的分發(fā)和執(zhí)行。事件循環(huán)機(jī)制由主線程和任務(wù)隊(duì)列構(gòu)成,主線程運(yùn)行完當(dāng)前任務(wù)后會(huì)檢查任務(wù)隊(duì)列中是否有待執(zhí)行的任務(wù),如有則執(zhí)行,否則等待2023-04-04純js+css實(shí)現(xiàn)仿移動(dòng)端淘寶網(wǎng)站的彈出詳情框功能
這篇文章主要介紹了純js+css實(shí)現(xiàn)仿移動(dòng)端淘寶網(wǎng)站的彈出詳情框功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12fckeditor粘貼Word時(shí)彈出窗口取消的方法
這篇文章主要介紹了fckeditor粘貼Word時(shí)彈出窗口取消的方法,是應(yīng)用fckeditor時(shí)非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10微信小程序搭建(mpvue+mpvue-weui+fly.js)的詳細(xì)步驟
這篇文章主要介紹了微信小程序搭建(mpvue+mpvue-weui+fly.js)的詳細(xì)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09詳解TypeScript2.0標(biāo)記聯(lián)合類型
這篇文章主要介紹了TypeScript2.0標(biāo)記聯(lián)合類型,對(duì)TS感興趣的同學(xué),可以參考下2021-05-05