DeukausGames

LoginScreen ES5 class

Apr 25th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Aplicação</title>
  6.     <style>
  7.         #app{
  8.             display: flex;
  9.             justify-content: center;
  10.             align-items: center;
  11.             flex-direction: column;
  12.             background-image: linear-gradient(90deg, red, blue);
  13.             height: 620px;
  14.         }
  15.         .item{
  16.             padding-inline: 61px;
  17.         }
  18.         .item2{
  19.             padding-inline: 49px;
  20.         }
  21.         #app button {
  22.             margin: 5px;
  23.         }
  24.         #app input {
  25.             margin: 5px;
  26.         }
  27.     </style>
  28. </head>
  29. <body>
  30.     <div id="app">
  31.         <input type="text" placeholder="digite seu email" name="email" />
  32.         <input type="text" placeholder="digite sua senha" name="senha" />
  33.         <button class="item" name="logar">Logar</button>
  34.         <button class="item2" name="cadastrar">Cadastrar</button>
  35.     </div>
  36.     <script>
  37.         "use strict";
  38.  
  39. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  40.  
  41. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  42.  
  43. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  44.  
  45. var App =
  46. /*#__PURE__*/
  47. function () {
  48.   //criar a classe app
  49.   function App() {
  50.     _classCallCheck(this, App);
  51.  
  52.     // __init__ do python
  53.     this.loguins = JSON.parse(localStorage.getItem('logins')) || [];
  54.     this.tt = [{
  55.       email: '[email protected]',
  56.       pw: '123456789'
  57.     }];
  58.  
  59.     if (this.loguins == []) {
  60.       localStorage.setItem('logins', JSON.stringify(this.tt));
  61.     }
  62.   }
  63.  
  64.   _createClass(App, [{
  65.     key: "logar",
  66.     value: function logar(email2, pw2) {
  67.       this.loguins = JSON.parse(localStorage.getItem('logins')) || [];
  68.  
  69.       for (var i = 0; i < this.loguins.length; i++) {
  70.         if (this.loguins[i].email === email2) {
  71.           if (this.loguins[i].pw == pw2) {
  72.             alert("Logado com sucesso!");
  73.             break;
  74.           } else {
  75.             alert("Senha incorreta!");
  76.             break;
  77.           }
  78.  
  79.           if (i == this.loguins.length - 1) {
  80.             if (this.loguins[i].email != email2) {
  81.               alert("Email incorreto!");
  82.             }
  83.           }
  84.         }
  85.       }
  86.     }
  87.   }, {
  88.     key: "registrar",
  89.     value: function registrar(email, pw) {
  90.       this.email1 = email;
  91.       this.pw1 = pw;
  92.       this.loguins = JSON.parse(localStorage.getItem('logins')) || [];
  93.       console.log(this.loguins);
  94.  
  95.       for (this.k = 0; this.k < this.loguins.length; this.k++) {
  96.         if (this.loguins[this.k].email == this.email1) {
  97.           alert('Este endereço de email ja existe!');
  98.           break;
  99.         }
  100.  
  101.         if (this.k == this.loguins.length - 1 && this.loguins[this.k].email != this.email1) {
  102.           this.loguins.push({
  103.             email: this.email1,
  104.             pw: this.pw1
  105.           });
  106.           localStorage.setItem('logins', JSON.stringify(this.loguins));
  107.           alert('Email e senha cadastrados!');
  108.           break;
  109.         }
  110.       }
  111.     }
  112.   }]);
  113.  
  114.   return App;
  115. }();
  116.  
  117. var iniciarApp = new App();
  118.  
  119. document.getElementsByName('cadastrar')[0].onclick = function () {
  120.   var textoEmail = document.getElementsByName('email');
  121.   var textoPw = document.getElementsByName('senha');
  122.  
  123.   if (textoEmail[0].value == '' || textoPw[0].value == '') {
  124.     alert('Os campos não podem estar vazios!');
  125.     textoEmail[0].value = '';
  126.     textoPw[0].value = '';
  127.   } else {
  128.     if (verificarvar(textoEmail[0].value)) {
  129.       if (textoPw[0].value.length < 5) {
  130.         alert('Insira uma senha mais forte!');
  131.         textoEmail[0].value = '';
  132.         textoPw[0].value = '';
  133.       } else {
  134.         iniciarApp.registrar(textoEmail[0].value, textoPw[0].value);
  135.         textoEmail[0].value = '';
  136.         textoPw[0].value = '';
  137.       }
  138.     } else {
  139.       alert('Insira um endereço de email valido!');
  140.       textoEmail[0].value = '';
  141.       textoPw[0].value = '';
  142.     }
  143.   }
  144. };
  145.  
  146. document.getElementsByName('logar')[0].onclick = function () {
  147.   var textoEmail = document.getElementsByName('email');
  148.   var textoPw = document.getElementsByName('senha');
  149.  
  150.   if (textoEmail[0].value == '' || textoPw[0].value == '') {
  151.     alert('Os campos não podem estar vazios!');
  152.     textoEmail[0].value = '';
  153.     textoPw[0].value = '';
  154.   } else {
  155.     if (verificarvar(textoEmail[0].value)) {
  156.       iniciarApp.logar(textoEmail[0].value, textoPw[0].value);
  157.       textoEmail[0].value = '';
  158.       textoPw[0].value = '';
  159.     } else {
  160.       alert('Insira um endereço de email valido!');
  161.       textoEmail[0].value = '';
  162.       textoPw[0].value = '';
  163.     }
  164.   }
  165. };
  166.  
  167. function verificarvar(txtmail) {
  168.   var vdd = false;
  169.  
  170.   for (var a = 0; a < txtmail.length; a++) {
  171.     if (txtmail[a] == '@') {
  172.       vdd = true;
  173.       break;
  174.     }
  175.   }
  176.  
  177.   return vdd == true ? true : false;
  178. }
  179.     </script>
  180. </body>
  181. </html>
Advertisement
Add Comment
Please, Sign In to add comment