注册页面
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script> <style> * { margin: 0; padding: 0; } a { text-decoration: none; } html { width: 100%; height: 100%; } body { background: rgb(138, 245, 124); } .user { width: 350px; margin: 200px auto 0; border: 1px #ccc solid; border-radius: 5px; background-color: rgb(252, 251, 246); padding: 30px 0; color: rgb(16, 17, 17) } .user div { width: 300px; margin: 25px auto; display: flex; justify-content: center; } input { outline: 0; font-size: 12px; } .username input, .password input { height: 20px; width: 224px; border: 1px rgb(148, 143, 143) solid; padding: 5px; } .code input { width: 100px; height: 20px; padding: 5px; border: 1px rgb(148, 143, 143) solid; } .code .yzm { display: block; height: 20px; width: 50px; padding: 5px; border: 1px rgb(148, 143, 143) solid; margin: 0 10px; background-color: #fff; color: #ccc; line-height: 20px; text-align: center; font-size: 8px; } .code button { border: none; outline: 0; background-color: skyblue; font-size: 14px; font-weight: 100; color: grey; cursor: pointer; } .user .btn-group { text-align: center; margin-top: 50px; } .btn-group a { height: 40px; line-height: 40px; width: 100px; background-color: rgba(255, 105, 248, 0.425); display: block; text-align: center; border-radius: 5px; border: none; outline: 0; cursor: pointer; margin: 0 20px; color: black; font-size: 14px; } </style></head><body> <div class="user"> <form> <div class="username"> <span>用户名:</span><input type="text" placeholder="以字母开头6-12位的数字和字母组合!" autocomplete="off"> </div> <div class="password"> <span>密码: </span><input type="password" placeholder="6-12位的字母和数组的组合!" autocomplete="off"> </div> </form> <div class="btn-group"> <a href="./record.html">登录</a> <a onclick="login()" class="sign">注册</a> </div> </div> <script> function login() { //点击事件 var name = $('.username input').val() //获取用户名 var password = $('.password input').val() //获取密码 if (!name.match(/^[a-zA-Z][0-9a-zA-Z]{5,11}$/)) { //正则表达式 alert('用户名只能是以字母开头6-12位的数字和字母组合') return } if (!password.match(/^[0-9a-zA-Z]{6,12}$/)) { alert('密码只能是6-12位的字母和数组的组合!') return } //方法1 // if (localStorage.getItem(name) == password) { // alert('用户名已注册') // } else { // localStorage.setItem(name, password) // alert('注册成功') // window.location.href = './record.html' // } //方法2 let ani = JSON.parse(localStorage.getItem('账号')) //获取localStorage存储的数据 let admin = [] //声明一个数组 if (ani != null) { //如果获取到的localStorage存储的数据不为空,则证明之前有数据,把之前的数据添加进数组admin let flag = true for (var i = 0; i < ani.length; i++) { //遍历获取localStorage存储的数据 if (ani[i].name == name) { //如果获取到localStorage存储的数据与输入的数据相同则弹出对话框,并跳出循环 alert('用户名已被注册') flag = false break; } else { admin.push(item) //如果不相同,则把之前的数据添加到admin中 } } admin.push({ name: name, password: password }) //如果不相同,并把输入的数据也添加到admin中 // console.log(admin) localStorage.setItem('账号', JSON.stringify(admin)) //重新存储到浏览器 if (flag == true) { alert('注册成功') window.location.href = './record.html' } } else { //如果获取localStorage存储的数据为空,则之前没有人注册则直接注册 admin.push({ name: name, password: password }) localStorage.setItem('账号', JSON.stringify(admin)) alert('注册成功') window.location.href = './record.html' } } </script></body></html>
登陆页面
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script> <style> * { margin: 0; padding: 0; } a { text-decoration: none; } html { width: 100%; height: 100%; } body { background: skyblue; } .user { width: 350px; margin: 200px auto 0; border: 1px #ccc solid; border-radius: 5px; background-color: rgb(231, 240, 243); padding: 30px 0; color: rgb(13, 14, 13); } .user div { width: 300px; margin: 25px auto; display: flex; justify-content: center; } input { outline: 0; font-size: 12px; } .username input, .password input { height: 20px; width: 224px; border: 1px rgb(148, 143, 143) solid; padding: 5px; } .code input { width: 100px; height: 20px; padding: 5px; border: 1px rgb(148, 143, 143) solid; } .code .yzm { display: block; height: 20px; width: 50px; padding: 5px; border: 1px rgb(148, 143, 143) solid; margin: 0 10px; background-color: #fff; color: #ccc; line-height: 20px; text-align: center; font-size: 8px; } .code button { border: none; outline: 0; background-color: skyblue; font-size: 14px; font-weight: 100; color: grey; cursor: pointer; } .user .btn-group { text-align: center; margin-top: 50px; } .btn-group a { height: 40px; width: 100px; background-color: rgba(255, 105, 248, 0.425); display: block; text-align: center; border-radius: 5px; border: none; outline: 0; cursor: pointer; margin: 0 20px; color: black; font-size: 14px; line-height: 40px; } .noLand { margin-left: 20px; font-size: 12px; } </style></head><body> <div class="user"> <form> <div class="username"> <span>用户名:</span><input type="text" placeholder="以字母开头6-12位的数字和字母组合!" autocomplete="off"> </div> <div class="password"> <span>密码: </span><input type="password" placeholder="6-12位的字母和数组的组合!" autocomplete="off"> </div> <p class="noLand"> <label for="male"> <input type="checkbox" id="male" checked> 7天免登录 </label> <!-- checked 利用checked来读取选项框被选中的状态 --> </p> </form> <div class="btn-group"> <a onclick="register()">登录</a> <a href="./register.html">注册</a> </div> </div> <script> let user = document.querySelector('.username input') let psd = document.querySelector('.password input') let male = document.getElementById('male') function register() { var name = $('.username input').val() var password = $('.password input').val() if (!name.match(/^[a-zA-Z][0-9a-zA-Z]{5,11}$/)) { alert('用户名只能是以字母开头6-12位的数字和字母组合') return } if (!password.match(/^[0-9a-zA-Z]{6,12}$/)) { alert('密码只能是6-12位的字母和数组的组合!') return } let ani = JSON.parse(localStorage.getItem('账号')) //取出浏览器数据 for (var i = 0; i < ani.length; i++) { //遍历数据 if (ani[i].name == name && ani[i].password == password) { //如果获取localStorage存储的数据的与输入的数据相同则登录成功,否则登录失败 let date = new Date() //设置新的事件 date.setTime(date.getTime() + 7 * 24 * 60 * 60 * 1000) //加上事件期限 date1 = date.toGMTString() //转换成格林时间 // console.log(ani[i].name) // console.log(ani[i].password) if (male.checked) { //如果7天免登陆为选中状态,则把localStorage的数据储存到cookie中 document.cookie = "admin=" + ani[i].name + ";expires=" + date1 document.cookie = "psw=" + ani[i].password + ";expires=" + date1 } window.location.href = 'https://www.bootcdn.cn/' alert('登录成功') } else { alert('用户名或密码错误') } } } window.onload = function() { //页面加载完毕自动运行 function gain() { if (document.cookie) { //如果cookie中有数据 let lists = document.cookie.split(';') user.setAttribute('value', lists[0].split('=')[1]) psd.setAttribute('value', lists[1].split('=')[1]) register() } } gain() } </script></body></html>