注册页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. a {
  15. text-decoration: none;
  16. }
  17. html {
  18. width: 100%;
  19. height: 100%;
  20. }
  21. body {
  22. background: rgb(138, 245, 124);
  23. }
  24. .user {
  25. width: 350px;
  26. margin: 200px auto 0;
  27. border: 1px #ccc solid;
  28. border-radius: 5px;
  29. background-color: rgb(252, 251, 246);
  30. padding: 30px 0;
  31. color: rgb(16, 17, 17)
  32. }
  33. .user div {
  34. width: 300px;
  35. margin: 25px auto;
  36. display: flex;
  37. justify-content: center;
  38. }
  39. input {
  40. outline: 0;
  41. font-size: 12px;
  42. }
  43. .username input,
  44. .password input {
  45. height: 20px;
  46. width: 224px;
  47. border: 1px rgb(148, 143, 143) solid;
  48. padding: 5px;
  49. }
  50. .code input {
  51. width: 100px;
  52. height: 20px;
  53. padding: 5px;
  54. border: 1px rgb(148, 143, 143) solid;
  55. }
  56. .code .yzm {
  57. display: block;
  58. height: 20px;
  59. width: 50px;
  60. padding: 5px;
  61. border: 1px rgb(148, 143, 143) solid;
  62. margin: 0 10px;
  63. background-color: #fff;
  64. color: #ccc;
  65. line-height: 20px;
  66. text-align: center;
  67. font-size: 8px;
  68. }
  69. .code button {
  70. border: none;
  71. outline: 0;
  72. background-color: skyblue;
  73. font-size: 14px;
  74. font-weight: 100;
  75. color: grey;
  76. cursor: pointer;
  77. }
  78. .user .btn-group {
  79. text-align: center;
  80. margin-top: 50px;
  81. }
  82. .btn-group a {
  83. height: 40px;
  84. line-height: 40px;
  85. width: 100px;
  86. background-color: rgba(255, 105, 248, 0.425);
  87. display: block;
  88. text-align: center;
  89. border-radius: 5px;
  90. border: none;
  91. outline: 0;
  92. cursor: pointer;
  93. margin: 0 20px;
  94. color: black;
  95. font-size: 14px;
  96. }
  97. </style>
  98. </head>
  99. <body>
  100. <div class="user">
  101. <form>
  102. <div class="username">
  103. <span>用户名:</span><input type="text" placeholder="以字母开头6-12位的数字和字母组合!" autocomplete="off">
  104. </div>
  105. <div class="password">
  106. <span>密码:&nbsp;&nbsp;&nbsp;</span><input type="password" placeholder="6-12位的字母和数组的组合!" autocomplete="off">
  107. </div>
  108. </form>
  109. <div class="btn-group">
  110. <a href="./record.html">登录</a>
  111. <a onclick="login()" class="sign">注册</a>
  112. </div>
  113. </div>
  114. <script>
  115. function login() { //点击事件
  116. var name = $('.username input').val() //获取用户名
  117. var password = $('.password input').val() //获取密码
  118. if (!name.match(/^[a-zA-Z][0-9a-zA-Z]{5,11}$/)) { //正则表达式
  119. alert('用户名只能是以字母开头6-12位的数字和字母组合')
  120. return
  121. }
  122. if (!password.match(/^[0-9a-zA-Z]{6,12}$/)) {
  123. alert('密码只能是6-12位的字母和数组的组合!')
  124. return
  125. }
  126. //方法1
  127. // if (localStorage.getItem(name) == password) {
  128. // alert('用户名已注册')
  129. // } else {
  130. // localStorage.setItem(name, password)
  131. // alert('注册成功')
  132. // window.location.href = './record.html'
  133. // }
  134. //方法2
  135. let ani = JSON.parse(localStorage.getItem('账号')) //获取localStorage存储的数据
  136. let admin = [] //声明一个数组
  137. if (ani != null) { //如果获取到的localStorage存储的数据不为空,则证明之前有数据,把之前的数据添加进数组admin
  138. let flag = true
  139. for (var i = 0; i < ani.length; i++) { //遍历获取localStorage存储的数据
  140. if (ani[i].name == name) { //如果获取到localStorage存储的数据与输入的数据相同则弹出对话框,并跳出循环
  141. alert('用户名已被注册')
  142. flag = false
  143. break;
  144. } else {
  145. admin.push(item) //如果不相同,则把之前的数据添加到admin中
  146. }
  147. }
  148. admin.push({
  149. name: name,
  150. password: password
  151. }) //如果不相同,并把输入的数据也添加到admin中
  152. // console.log(admin)
  153. localStorage.setItem('账号', JSON.stringify(admin)) //重新存储到浏览器
  154. if (flag == true) {
  155. alert('注册成功')
  156. window.location.href = './record.html'
  157. }
  158. } else { //如果获取localStorage存储的数据为空,则之前没有人注册则直接注册
  159. admin.push({
  160. name: name,
  161. password: password
  162. })
  163. localStorage.setItem('账号', JSON.stringify(admin))
  164. alert('注册成功')
  165. window.location.href = './record.html'
  166. }
  167. }
  168. </script>
  169. </body>
  170. </html>

登陆页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. a {
  15. text-decoration: none;
  16. }
  17. html {
  18. width: 100%;
  19. height: 100%;
  20. }
  21. body {
  22. background: skyblue;
  23. }
  24. .user {
  25. width: 350px;
  26. margin: 200px auto 0;
  27. border: 1px #ccc solid;
  28. border-radius: 5px;
  29. background-color: rgb(231, 240, 243);
  30. padding: 30px 0;
  31. color: rgb(13, 14, 13);
  32. }
  33. .user div {
  34. width: 300px;
  35. margin: 25px auto;
  36. display: flex;
  37. justify-content: center;
  38. }
  39. input {
  40. outline: 0;
  41. font-size: 12px;
  42. }
  43. .username input,
  44. .password input {
  45. height: 20px;
  46. width: 224px;
  47. border: 1px rgb(148, 143, 143) solid;
  48. padding: 5px;
  49. }
  50. .code input {
  51. width: 100px;
  52. height: 20px;
  53. padding: 5px;
  54. border: 1px rgb(148, 143, 143) solid;
  55. }
  56. .code .yzm {
  57. display: block;
  58. height: 20px;
  59. width: 50px;
  60. padding: 5px;
  61. border: 1px rgb(148, 143, 143) solid;
  62. margin: 0 10px;
  63. background-color: #fff;
  64. color: #ccc;
  65. line-height: 20px;
  66. text-align: center;
  67. font-size: 8px;
  68. }
  69. .code button {
  70. border: none;
  71. outline: 0;
  72. background-color: skyblue;
  73. font-size: 14px;
  74. font-weight: 100;
  75. color: grey;
  76. cursor: pointer;
  77. }
  78. .user .btn-group {
  79. text-align: center;
  80. margin-top: 50px;
  81. }
  82. .btn-group a {
  83. height: 40px;
  84. width: 100px;
  85. background-color: rgba(255, 105, 248, 0.425);
  86. display: block;
  87. text-align: center;
  88. border-radius: 5px;
  89. border: none;
  90. outline: 0;
  91. cursor: pointer;
  92. margin: 0 20px;
  93. color: black;
  94. font-size: 14px;
  95. line-height: 40px;
  96. }
  97. .noLand {
  98. margin-left: 20px;
  99. font-size: 12px;
  100. }
  101. </style>
  102. </head>
  103. <body>
  104. <div class="user">
  105. <form>
  106. <div class="username">
  107. <span>用户名:</span><input type="text" placeholder="以字母开头6-12位的数字和字母组合!" autocomplete="off">
  108. </div>
  109. <div class="password">
  110. <span>密码:&nbsp;&nbsp;&nbsp;</span><input type="password" placeholder="6-12位的字母和数组的组合!" autocomplete="off">
  111. </div>
  112. <p class="noLand">
  113. <label for="male">
  114. <input type="checkbox" id="male" checked> 7天免登录
  115. </label>
  116. <!-- checked 利用checked来读取选项框被选中的状态 -->
  117. </p>
  118. </form>
  119. <div class="btn-group">
  120. <a onclick="register()">登录</a>
  121. <a href="./register.html">注册</a>
  122. </div>
  123. </div>
  124. <script>
  125. let user = document.querySelector('.username input')
  126. let psd = document.querySelector('.password input')
  127. let male = document.getElementById('male')
  128. function register() {
  129. var name = $('.username input').val()
  130. var password = $('.password input').val()
  131. if (!name.match(/^[a-zA-Z][0-9a-zA-Z]{5,11}$/)) {
  132. alert('用户名只能是以字母开头6-12位的数字和字母组合')
  133. return
  134. }
  135. if (!password.match(/^[0-9a-zA-Z]{6,12}$/)) {
  136. alert('密码只能是6-12位的字母和数组的组合!')
  137. return
  138. }
  139. let ani = JSON.parse(localStorage.getItem('账号')) //取出浏览器数据
  140. for (var i = 0; i < ani.length; i++) { //遍历数据
  141. if (ani[i].name == name && ani[i].password == password) { //如果获取localStorage存储的数据的与输入的数据相同则登录成功,否则登录失败
  142. let date = new Date() //设置新的事件
  143. date.setTime(date.getTime() + 7 * 24 * 60 * 60 * 1000) //加上事件期限
  144. date1 = date.toGMTString() //转换成格林时间
  145. // console.log(ani[i].name)
  146. // console.log(ani[i].password)
  147. if (male.checked) { //如果7天免登陆为选中状态,则把localStorage的数据储存到cookie中
  148. document.cookie = "admin=" + ani[i].name + ";expires=" + date1
  149. document.cookie = "psw=" + ani[i].password + ";expires=" + date1
  150. }
  151. window.location.href = 'https://www.bootcdn.cn/'
  152. alert('登录成功')
  153. } else {
  154. alert('用户名或密码错误')
  155. }
  156. }
  157. }
  158. window.onload = function() { //页面加载完毕自动运行
  159. function gain() {
  160. if (document.cookie) { //如果cookie中有数据
  161. let lists = document.cookie.split(';')
  162. user.setAttribute('value', lists[0].split('=')[1])
  163. psd.setAttribute('value', lists[1].split('=')[1])
  164. register()
  165. }
  166. }
  167. gain()
  168. }
  169. </script>
  170. </body>
  171. </html>