DeukausGames

LoginScreen ES6 class

Apr 25th, 2019
407
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.         class App { //criar a classe app
  38.     constructor() { // __init__ do python
  39.         this.loguins = JSON.parse(localStorage.getItem('logins')) || [];
  40.         this.tt = [{email: '[email protected]', pw: '123456789'}]
  41.         if (this.loguins == []) {
  42.             localStorage.setItem('logins', JSON.stringify(this.tt))
  43.         }
  44.     }
  45.    
  46.     logar(email2, pw2) {
  47.         this.loguins = JSON.parse(localStorage.getItem('logins')) || [];
  48.         for (var i = 0; i < this.loguins.length; i++) {
  49.             if (this.loguins[i].email === email2) {
  50.                 if (this.loguins[i].pw == pw2) {
  51.                     alert("Logado com sucesso!");
  52.                     break;
  53.                 } else {
  54.                     alert("Senha incorreta!");
  55.                     break;
  56.                 }
  57.             if (i == this.loguins.length-1) {
  58.                 if (this.loguins[i].email != email2) {
  59.                     alert("Email incorreto!");
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66.     registrar(email, pw) {
  67.         this.email1 = email;
  68.         this.pw1 = pw;
  69.         this.loguins = JSON.parse(localStorage.getItem('logins')) || [];
  70.         console.log(this.loguins);
  71.         for (this.k = 0; this.k < this.loguins.length; this.k++) {
  72.             if (this.loguins[this.k].email == this.email1) {
  73.                 alert('Este endereço de email ja existe!');
  74.                 break;
  75.             }
  76.             if (this.k == this.loguins.length-1 && this.loguins[this.k].email != this.email1) {
  77.                 this.loguins.push({
  78.                             email: this.email1,
  79.                             pw: this.pw1,
  80.                         });
  81.                 localStorage.setItem('logins', JSON.stringify(this.loguins));
  82.                 alert('Email e senha cadastrados!')
  83.                 break;
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. const iniciarApp = new App();
  90.  
  91. document.getElementsByName('cadastrar')[0].onclick = function() {
  92.     var textoEmail = document.getElementsByName('email');
  93.     var textoPw = document.getElementsByName('senha');
  94.     if (textoEmail[0].value == '' || textoPw[0].value == '') {
  95.         alert('Os campos não podem estar vazios!')
  96.         textoEmail[0].value = '';textoPw[0].value = '';
  97.     } else {
  98.         if (verificarvar(textoEmail[0].value)) {
  99.             if (textoPw[0].value.length < 5) {
  100.                 alert('Insira uma senha mais forte!');
  101.                 textoEmail[0].value = '';textoPw[0].value = '';
  102.             } else {
  103.                 iniciarApp.registrar(textoEmail[0].value, textoPw[0].value);
  104.                 textoEmail[0].value = '';textoPw[0].value = '';
  105.             }
  106.         } else {
  107.             alert('Insira um endereço de email valido!');
  108.             textoEmail[0].value = '';textoPw[0].value = '';
  109.         }
  110.     }
  111. }
  112.  
  113. document.getElementsByName('logar')[0].onclick = function() {
  114.     var textoEmail = document.getElementsByName('email');
  115.     var textoPw = document.getElementsByName('senha');
  116.     if (textoEmail[0].value == '' || textoPw[0].value == '') {
  117.         alert('Os campos não podem estar vazios!');
  118.         textoEmail[0].value = '';textoPw[0].value = '';
  119.     } else {
  120.         if (verificarvar(textoEmail[0].value)) {
  121.             iniciarApp.logar(textoEmail[0].value, textoPw[0].value);
  122.             textoEmail[0].value = '';textoPw[0].value = '';
  123.         } else {
  124.             alert('Insira um endereço de email valido!')
  125.             textoEmail[0].value = '';textoPw[0].value = '';
  126.         }
  127.     }
  128. }
  129.  
  130. function verificarvar(txtmail) {
  131.     let vdd = false;
  132.     for (var a = 0; a < txtmail.length; a++){
  133.         if (txtmail[a] == '@') {
  134.             vdd = true;
  135.             break;
  136.         }
  137.     }
  138.     return (vdd == true) ? true: false;
  139. }  
  140.     </script>
  141. </body>
  142. </html>
Advertisement
Add Comment
Please, Sign In to add comment