最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)本地持久化存儲(chǔ)登錄注冊(cè)

 更新時(shí)間:2022年08月25日 10:46:02   作者:小滿blue  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)本地持久化存儲(chǔ)登錄注冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)本地持久化存儲(chǔ)登錄注冊(cè)的具體代碼,供大家參考,具體內(nèi)容如下

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="登錄賬號(hào)">
? ? ? ? <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="注冊(cè)">
? ? </div>
? ? <script src="js/login.js"></script>
</body>
</html>

2.注冊(cè)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>注冊(cè)界面</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>注冊(cè)界面</h2>
? ? ? ? <input type="text" name="registerId" placeholder="設(shè)置賬號(hào)">
? ? ? ? <div class="text registerIdText"></div>
? ? ? ? <input type="password" name="registerPassword" placeholder="設(shè)置密碼">
? ? ? ? <div class="text registerPasswordText" ></div>
? ? ? ? <input class="btn" type="submit" value="注冊(cè)">
? ? ? ? <input class="btn toLoginBtn" type="button" value="返回登錄">
? ? </div>
? ? <script src="js/register.js"></script>
</body>
</html>

3.登錄界面js文件

const isHaveId = () => {
? ? let loginId = loginIdEle.value
? ? //遍歷存儲(chǔ)的信息,如果有id相同的則返回true
? ? let idHave=message.some(item => loginId == item.id)
? ? //如果結(jié)果為true,顯示內(nèi)容為空,否則,顯示賬號(hào)不存在
? ? if (idHave==true) {
? ? ? ? loginIdText.innerHTML = ''
? ? ? ? return true
? ? } else {
? ? ? ? loginIdText.innerHTML = '賬號(hào)不存在!'
? ? ? ? return false
? ? }
}
//驗(yàn)證密碼
const isHavePassword = () => {
? ? let loginPassword = loginPasswordEle.value
? ? let passwordHave=message.some(item =>loginPassword == item.password )
? ? ? ? if (passwordHave==false) {
? ? ? ? ? ? loginPasswordText.innerHTML = '密碼錯(cuò)誤!'
? ? ? ? ? ? return false
? ? ? ? } else {
? ? ? ? ? ? loginPasswordText.innerHTML = ''
? ? ? ? ? ? return true
? ? ? ? }
}
//點(diǎn)擊事件
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 = ''
}
?
//焦點(diǎn)事件
loginIdEle.addEventListener('blur', function () {
? ? let isEmptyChecjedId = emptyChecjedId()
? ? if (!isEmptyChecjedId) {
? ? ? ? return
? ? }
? ? isHaveId()
?
})
//密碼焦點(diǎn)事件
loginPasswordEle.addEventListener('blur', function () {
? ? let isEmptyChecjedPassword = emptyChecjedPassword()
? ? //如果為空,則不進(jìn)行強(qiáng)度驗(yàn)證,不為空時(shí),再進(jìn)行強(qiáng)度驗(yàn)證
? ? if (!isEmptyChecjedPassword) {
? ? ? ? return
? ? }
? ? isHavePassword()
})
//點(diǎn)擊注冊(cè)跳轉(zhuǎn)到注冊(cè)界面
function toRegister() {
? ? const toRegisterEle = document.querySelector('.registerBtn')
? ? toRegisterEle.onclick = function () {
? ? ? ? location.href = './register.html'
? ? }
}
toRegister()

4.注冊(cè)界面js文件

//獲取節(jié)點(diǎn)
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')
//賬號(hào)非空驗(yàn)證
const emptyChecjedId = () => {
? ? //獲取節(jié)點(diǎn)內(nèi)容
? ? let registerId = registerIdEle.value
? ? if (registerId == '') {
? ? ? ? registerIdText.innerHTML = '用戶名不能為空!'
? ? ? ? return false
? ? } else {
? ? ? ? registerIdText.innerHTML = ''
? ? ? ? return true
? ? }
}
//密碼非空驗(yàn)證
const emptyChecjedPassword=()=>{
? ? let registerPassword = registerPasswordEle.value
? ? if(registerPassword==''){
? ? ? ? registerPasswordText.innerHTML='密碼不能為空!'
? ? ? ? return false
? ? }else{
? ? ? ? registerPasswordText.innerHTML=''
? ? ? ? return true
? ? }
}
?
//密碼強(qiáng)度驗(yàn)證
const passwordDegree = () => {
? ? let password = registerPasswordEle.value
? ? let reg = /^[A-Z][0-9a-zA-Z]{7}/
? ? if (!reg.test(password)) {
? ? ? ? registerPasswordText.innerHTML = '密碼必須以大寫字母開頭,至少8位字母或數(shù)字!'
? ? ? ? return false
? ? } else {
? ? ? ? registerPasswordText.innerHTML = ''
? ? ? ? return true
? ? }
}
//點(diǎn)擊事件
registerBtnEle.onclick=function(){
? ? let isEmptyChecjedId=emptyChecjedId()
? ? let isEmptyChecjedPassword=emptyChecjedPassword()
? ? if(!isEmptyChecjedId||!isEmptyChecjedPassword){
? ? ? ? return
? ? }
? ? //密碼強(qiáng)度
? ? let isPasswordDegree=passwordDegree()
? ? if(!isPasswordDegree){
? ? ? ? return
? ? }
? ? alert('注冊(cè)成功!')
? ? //將數(shù)據(jù)持久化存儲(chǔ)
? ? 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=''
}
//焦點(diǎn)事件
registerIdEle.addEventListener('blur', function () {
? ? emptyChecjedId()
})
//密碼焦點(diǎn)事件
registerPasswordEle.addEventListener('blur', function () {
? ? let isEmptyChecjedPassword=emptyChecjedPassword()
? ? //如果為空,則不進(jìn)行強(qiáng)度驗(yàn)證,不為空時(shí),再進(jìn)行強(qiáng)度驗(yàn)證
? ? if (!isEmptyChecjedPassword) {
? ? ? ? return
? ? }
? ? passwordDegree()
})
function toLogin(){
? ? const toLoginEle=document.querySelector('.toLoginBtn')
? ? toLoginEle.onclick=function(){
? ? ? ? location.href='./login.html'
? ? }
}
toLogin()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

南华县| 深州市| 浮梁县| 洛阳市| 塔河县| 五峰| 沅江市| 徐汇区| 印江| 五常市| 定远县| 宁南县| 子洲县| 上饶县| 华坪县| 太谷县| 呼玛县| 虎林市| 南通市| 酉阳| 忻州市| 沛县| 当雄县| 霍城县| 酉阳| 兴宁市| 资中县| 恭城| 峡江县| 吴川市| 荣成市| 溧阳市| 湖北省| 靖江市| 通许县| 堆龙德庆县| 泊头市| 会理县| 陕西省| 蓬安县| 凤庆县|