87 lines
2.4 KiB
Markdown
87 lines
2.4 KiB
Markdown
---
|
|
id: bad87fee1348bd9aedc08830
|
|
title: Usare HTML5 per rendere obbligatorio un campo
|
|
challengeType: 0
|
|
videoUrl: 'https://scrimba.com/p/pVMPUv/cMd4EcQ'
|
|
forumTopicId: 18360
|
|
dashedName: use-html5-to-require-a-field
|
|
---
|
|
|
|
# --description--
|
|
|
|
È possibile rendere obbligatori specifici campi di un modulo in modo che l'utente non sia in grado di inviarlo fino a quando non li abbia compilati.
|
|
|
|
Per esempio, se volessi rendere obbligatorio un campo di immissione di testo, potresti semplicemente aggiungere l'attributo `required` all'interno del tuo elemento `input`, in questo modo: `<input type="text" required>`
|
|
|
|
# --instructions--
|
|
|
|
Rendi il tuo testo `input` un campo `required`, in modo che il tuo utente non possa inviare il modulo senza completare questo campo.
|
|
|
|
Quindi prova a inviare il modulo senza inserire alcun testo. Vedi come il tuo modulo HTML5 ti informa che il campo è obbligatorio?
|
|
|
|
# --hints--
|
|
|
|
Il tuo elemento `input` di testo dovrebbe avere l'attributo `required`.
|
|
|
|
```js
|
|
assert($('input').prop('required'));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```html
|
|
<h2>CatPhotoApp</h2>
|
|
<main>
|
|
<p>Click here to view more <a href="#">cat photos</a>.</p>
|
|
|
|
<a href="#"><img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt="A cute orange cat lying on its back."></a>
|
|
|
|
<p>Things cats love:</p>
|
|
<ul>
|
|
<li>cat nip</li>
|
|
<li>laser pointers</li>
|
|
<li>lasagna</li>
|
|
</ul>
|
|
<p>Top 3 things cats hate:</p>
|
|
<ol>
|
|
<li>flea treatment</li>
|
|
<li>thunder</li>
|
|
<li>other cats</li>
|
|
</ol>
|
|
<form action="https://www.freecatphotoapp.com/submit-cat-photo">
|
|
<input type="text" placeholder="cat photo URL">
|
|
<button type="submit">Submit</button>
|
|
</form>
|
|
</main>
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```html
|
|
<h2>CatPhotoApp</h2>
|
|
<main>
|
|
<p>Click here to view more <a href="#">cat photos</a>.</p>
|
|
|
|
<a href="#"><img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt="A cute orange cat lying on its back."></a>
|
|
|
|
<p>Things cats love:</p>
|
|
<ul>
|
|
<li>cat nip</li>
|
|
<li>laser pointers</li>
|
|
<li>lasagna</li>
|
|
</ul>
|
|
<p>Top 3 things cats hate:</p>
|
|
<ol>
|
|
<li>flea treatment</li>
|
|
<li>thunder</li>
|
|
<li>other cats</li>
|
|
</ol>
|
|
<form action="https://www.freecatphotoapp.com/submit-cat-photo">
|
|
<input type="text" required placeholder="cat photo URL">
|
|
<button type="submit">Submit</button>
|
|
</form>
|
|
</main>
|
|
```
|