js實現本地持久化存儲登錄註冊
本文實例為大傢分享瞭js實現本地持久化存儲登錄註冊的具體代碼,供大傢參考,具體內容如下
1.登錄html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>登錄界面</title> <style> .container { text-align: center; margin: 50px auto; width: 600px; height: 400px; background-color: rgb(201, 208, 247); box-sizing: border-box; padding-top: 20px; } .container input { width: 400px; height: 30px; border: none; list-style: none; } .container .btn { width: 404px; height: 35px; margin-top: 10px; border: none; background-color: deepskyblue; color: white; border-radius: 5px; cursor: pointer; } .container .text { margin-top: 10px; height: 20px; width: 400px; font-size: 12px; color: tomato; } </style> </head> <body> <div class="container"> <h2>登錄界面</h2> <input type="text" name="loginId" placeholder="登錄賬號"> <div class="text loginIdText"></div> <input type="password" name="loginPassword" placeholder="登錄密碼"> <div class="text loginPasswordText"></div> <input class="btn loginBtn" type="submit" value="登錄"> <input class="btn registerBtn" type="submit" value="註冊"> </div> <script src="js/login.js"></script> </body> </html>
2.註冊html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>註冊界面</title> <style> .container{ text-align: center; margin: 50px auto; width: 600px; height: 400px; background-color: rgb(201, 208, 247); box-sizing: border-box; padding-top: 20px; } .container input{ width: 400px; height: 30px; border: none; list-style: none; } .container .btn{ width: 404px; height: 35px; margin-top: 10px; border: none; background-color: deepskyblue; color: white; border-radius: 5px; cursor: pointer; } .container .text{ margin-top: 10px; height: 20px; width: 400px; font-size: 12px; color: tomato; } </style> </head> <body> <div class="container"> <h2>註冊界面</h2> <input type="text" name="registerId" placeholder="設置賬號"> <div class="text registerIdText"></div> <input type="password" name="registerPassword" placeholder="設置密碼"> <div class="text registerPasswordText" ></div> <input class="btn" type="submit" value="註冊"> <input class="btn toLoginBtn" type="button" value="返回登錄"> </div> <script src="js/register.js"></script> </body> </html>
3.登錄界面js文件
const isHaveId = () => { let loginId = loginIdEle.value //遍歷存儲的信息,如果有id相同的則返回true let idHave=message.some(item => loginId == item.id) //如果結果為true,顯示內容為空,否則,顯示賬號不存在 if (idHave==true) { loginIdText.innerHTML = '' return true } else { loginIdText.innerHTML = '賬號不存在!' return false } } //驗證密碼 const isHavePassword = () => { let loginPassword = loginPasswordEle.value let passwordHave=message.some(item =>loginPassword == item.password ) if (passwordHave==false) { loginPasswordText.innerHTML = '密碼錯誤!' return false } else { loginPasswordText.innerHTML = '' return true } } //點擊事件 loginBtn.onclick = function () { let isEmptyChecjedId = emptyChecjedId() let isEmptyChecjedPassword = emptyChecjedPassword() if (!isEmptyChecjedId || !isEmptyChecjedPassword) { return } let idHave = isHaveId() let passwordHave = isHavePassword() if (!idHave ||!passwordHave ) { return } alert('登錄成功!') loginIdEle.value = '' loginPasswordEle.value = '' } //焦點事件 loginIdEle.addEventListener('blur', function () { let isEmptyChecjedId = emptyChecjedId() if (!isEmptyChecjedId) { return } isHaveId() }) //密碼焦點事件 loginPasswordEle.addEventListener('blur', function () { let isEmptyChecjedPassword = emptyChecjedPassword() //如果為空,則不進行強度驗證,不為空時,再進行強度驗證 if (!isEmptyChecjedPassword) { return } isHavePassword() }) //點擊註冊跳轉到註冊界面 function toRegister() { const toRegisterEle = document.querySelector('.registerBtn') toRegisterEle.onclick = function () { location.href = './register.html' } } toRegister()
4.註冊界面js文件
//獲取節點 const registerIdEle = document.querySelector('input[name="registerId"]') const registerPasswordEle = document.querySelector('input[name="registerPassword"]') const registerBtnEle = document.querySelector('.btn') const registerIdText=document.querySelector('.registerIdText') const registerPasswordText=document.querySelector('.registerPasswordText') //賬號非空驗證 const emptyChecjedId = () => { //獲取節點內容 let registerId = registerIdEle.value if (registerId == '') { registerIdText.innerHTML = '用戶名不能為空!' return false } else { registerIdText.innerHTML = '' return true } } //密碼非空驗證 const emptyChecjedPassword=()=>{ let registerPassword = registerPasswordEle.value if(registerPassword==''){ registerPasswordText.innerHTML='密碼不能為空!' return false }else{ registerPasswordText.innerHTML='' return true } } //密碼強度驗證 const passwordDegree = () => { let password = registerPasswordEle.value let reg = /^[A-Z][0-9a-zA-Z]{7}/ if (!reg.test(password)) { registerPasswordText.innerHTML = '密碼必須以大寫字母開頭,至少8位字母或數字!' return false } else { registerPasswordText.innerHTML = '' return true } } //點擊事件 registerBtnEle.onclick=function(){ let isEmptyChecjedId=emptyChecjedId() let isEmptyChecjedPassword=emptyChecjedPassword() if(!isEmptyChecjedId||!isEmptyChecjedPassword){ return } //密碼強度 let isPasswordDegree=passwordDegree() if(!isPasswordDegree){ return } alert('註冊成功!') //將數據持久化存儲 let message={ id:registerIdEle.value, password:registerPasswordEle.value } let messageStr=localStorage.getItem('userMessage') let messages=JSON.parse(messageStr) ||[] messages.push(message) localStorage.setItem('userMessage',JSON.stringify(messages)) registerIdEle.value='' registerPasswordEle.value='' } //焦點事件 registerIdEle.addEventListener('blur', function () { emptyChecjedId() }) //密碼焦點事件 registerPasswordEle.addEventListener('blur', function () { let isEmptyChecjedPassword=emptyChecjedPassword() //如果為空,則不進行強度驗證,不為空時,再進行強度驗證 if (!isEmptyChecjedPassword) { return } passwordDegree() }) function toLogin(){ const toLoginEle=document.querySelector('.toLoginBtn') toLoginEle.onclick=function(){ location.href='./login.html' } } toLogin()
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- JavaScript DOM API的使用教程及綜合案例
- 淺析js中事件冒泡與事件捕獲
- JavaScript操作元素教你改變頁面內容樣式
- Python全棧之學習JS(3)
- HTML+CSS+JS實現的簡單應用小案例分享