F. index.html

1
<!DOCTYPE html>
2
<html lang="es" class="light dark">
3
4
<head>
5
6
 <meta charset="UTF-8">
7
8
 <title>PWA con Formulario</title>
9
10
 <meta name="description" content="Ejemplo de PWA con Formulario">
11
12
 <!-- <script type="module" src="js/lib/registraServiceWorker.js"></script> -->
13
 <script type="module" src="js/lib/manejaErrores.js"></script>
14
15
 <meta name="viewport" content="width=device-width">
16
 <meta name="theme-color" content="#fffbfe">
17
 <link rel="icon" sizes="32x32" href="favicon.ico">
18
 <link rel="manifest" href="site.webmanifest">
19
 <script src="ungap/custom-elements.js"></script>
20
21
 <script type="module" src="js/lib/custom/md-app-bar.js"></script>
22
 <script type="module" src="js/nav-tab-fixed.js"></script>
23
24
 <link rel="stylesheet" href="css/estilos.css">
25
 <link rel="stylesheet" href="css/transicion_pestanas.css">
26
 <link rel="stylesheet" href="css/md-tab.css">
27
 <link rel="stylesheet" href="css/material-symbols-outlined.css">
28
 <link rel="stylesheet" href="css/md-filled-text-field.css">
29
 <link rel="stylesheet" href="css/md-filled-button.css">
30
 <link rel="stylesheet" href="css/md-outline-button.css">
31
32
</head>
33
34
<body>
35
36
 <form id="formulario" novalidate>
37
38
  <md-app-bar class="centered" adicional="tab">
39
40
   <h1>PWA con Formulario</h1>
41
42
  </md-app-bar>
43
44
  <nav-tab-fixed id="tab"></nav-tab-fixed>
45
46
  <section>
47
48
   <!-- Usa
49
    class="float"
50
   cuando quieras que la etiqueta esté arriba todo el tiempo. -->
51
   <p>
52
    <label class="md-filled-text-field float">
53
     <output name="respuesta">Todavía no hay respuesta</output>
54
     <span>Respuesta</span>
55
    </label>
56
   </p>
57
58
   <p>
59
    <label class="md-filled-text-field">
60
     <input name="nombre" required placeholder="Nombre*">
61
     <span>Nombre *</span>
62
     <small id="supportingNombre">Obligatorio</small>
63
    </label>
64
   </p>
65
66
   <p>
67
    <label class="md-filled-text-field">
68
     <input name="email" type="email" placeholder="Email">
69
     <span accesskey="M">Email</span>
70
     <small id="supportingEmail">Texto con formato de email</small>
71
    </label>
72
   </p>
73
74
   <p>
75
    <label class="md-filled-text-field float">
76
     <input name="fecha" type="date" placeholder="Fecha">
77
     <span>Fecha de nacimiento</span>
78
    </label>
79
   </p>
80
81
   <p>
82
    <label class="md-filled-text-field">
83
     <textarea name="dieccion" rows="3" placeholder="Dirección"></textarea>
84
     <span>Dirección</span>
85
    </label>
86
   </p>
87
88
   <p>
89
    <button id="botonSaludo" class="md-filled-button">Saludar</button>
90
    <button id="botonDatos" class="md-outline-button">Datos</button>
91
   </p>
92
93
  </section>
94
95
 </form>
96
97
 <script type="module">
98
99
  import { muestraTextoDeAyuda } from "./js/lib/muestraTextoDeAyuda.js"
100
101
  formulario.nombre.addEventListener("input", copiaMensajes)
102
  formulario.email.addEventListener("input", copiaMensajes)
103
  formulario.addEventListener("submit", procesa)
104
105
  function copiaMensajes() {
106
   muestraTextoDeAyuda(formulario.nombre, supportingNombre, "Obligatorio")
107
   muestraTextoDeAyuda(
108
    formulario.email, supportingEmail, "Texto con formato de email"
109
   )
110
  }
111
112
  /**
113
   * @param {SubmitEvent} evt
114
   */
115
  function procesa(evt) {
116
   evt.preventDefault()
117
   copiaMensajes()
118
   if (
119
    formulario.nombre.validity.valid
120
    && formulario.email.validity.valid
121
    && formulario.fecha.validity.valid
122
    && formulario.dieccion.validity.valid
123
   ) {
124
    const botonSeleccionado = evt.submitter
125
    if (botonSeleccionado === botonSaludo) {
126
     saluda()
127
    } else if (botonSeleccionado === botonDatos) {
128
     datos()
129
    }
130
   }
131
  }
132
133
  function saluda() {
134
   formulario.respuesta.value = `Hola ${formulario.nombre.value}`
135
  }
136
137
  function datos() {
138
   formulario.respuesta.value =
139
    `Nombre: ${formulario.nombre.value} Email: ${formulario.email.value}`
140
  }
141
142
 </script>
143
144
</body>
145
146
</html>
skip_previous skip_next