Formularios avanzados
El navegador ya sabe validar la mayoría de los casos comunes. Sin una línea de JavaScript.
Advanced forms
The browser already knows how to validate most common cases. Without a line of JavaScript.
El atributo type de un <input> no es solo cosmético: cada
tipo trae su propio teclado en celular y su propia validación básica gratis.
An <input>'s type attribute is not just cosmetic: each type
brings its own mobile keyboard and its own basic validation, for free.
Estos atributos le agregan reglas extra a cualquier campo: required lo hace
obligatorio, minlength/maxlength limitan el largo del texto, y
pattern exige una expresión regular —lo que ya viste en el módulo de
JavaScript—.
These attributes add extra rules to any field: required makes it mandatory,
minlength/maxlength limit the text's length, and
pattern requires a regular expression — the thing you already saw in the
JavaScript module.
El atributo title en el segundo campo es lo que el
navegador muestra como pista cuando el patrón no calza. No es opcional en la práctica: sin
eso, la persona solo ve "esto no cumple el formato" y no sabe cuál es.
The title attribute on the second field is what the
browser shows as a hint when the pattern does not match. It is not really optional: without
it, the person only sees "this does not match the format" with no idea what the format is.
CSS puede reaccionar solo, sin JavaScript, cuando un campo no cumple sus reglas: el
pseudo-selector :invalid se activa automáticamente.
CSS can react on its own, with no JavaScript, when a field breaks its rules: the
:invalid pseudo-selector activates automatically.
Ya usaste <label> envolviendo el input, que funciona. Pero la forma más
flexible es conectarlos con for y un id que coincida: así el label
puede ir en cualquier parte y el clic en el texto sigue enfocando el campo.
You already used <label> wrapping the input, which works. But the more
flexible way is connecting them with for and a matching id: that
way the label can go anywhere and clicking the text still focuses the field.
Tipos de input con validación incluida, required/pattern para
reglas extra, :invalid para darles estilo sin JavaScript, y por qué conectar
label con for/id importa de verdad.
Justamente de eso trata lo que sigue: hacer que tu página sea usable por cualquiera, no solo por quien ve y usa mouse.
Input types with built-in validation, required/pattern for extra
rules, :invalid to style them without JavaScript, and why connecting
label with for/id genuinely matters.
Which leads right into what comes next: making your page usable by anyone, not just people who can see and use a mouse.