87 lines
2.5 KiB
Markdown
87 lines
2.5 KiB
Markdown
---
|
|
id: bad87fee1348bd9aedc08830
|
|
title: HTML5 を使用してフィールドを必須にする
|
|
challengeType: 0
|
|
videoUrl: 'https://scrimba.com/p/pVMPUv/cMd4EcQ'
|
|
forumTopicId: 18360
|
|
dashedName: use-html5-to-require-a-field
|
|
---
|
|
|
|
# --description--
|
|
|
|
ユーザーがその項目を入力するまでフォームを送信できないように、特定のフォームフィールドを必須とすることができます。
|
|
|
|
例えば、テキスト入力フィールドを必須にしたい場合、次のように `required` 属性を `input` 要素に追加します: `<input type="text" required>`
|
|
|
|
# --instructions--
|
|
|
|
テキスト入力フィールド `input` を必須 `required` にして、ユーザーがこのフィールドを入力しなければフォームを送信できないようにしてください。
|
|
|
|
そして、テキストを入力せずにフォームを送信してみてください。 HTML5 のフォームがどのようにしてそのフィールドが必須であることを知らせるか確認できましたか?
|
|
|
|
# --hints--
|
|
|
|
テキスト入力フィールドの `input` 要素に `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>
|
|
```
|