aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/htmlinputelement/invalid_event/index.html
blob: c0e7d95f60443b59df3209ae51c24945cf91499e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
title: 'HTMLInputElement: invalid イベント'
slug: Web/API/HTMLInputElement/invalid_event
tags:
  - API
  - Constraint Validation API
  - Constraint validation
  - Event
  - Forms
  - Reference
  - invalid
translation_of: Web/API/HTMLInputElement/invalid_event
---
<p>{{APIRef}}</p>

<p><strong><code>invalid</code></strong> イベントは、送信可能な要素が制約の検証を受け、制約を満たしていない場合に発行されます。</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">バブリング</th>
   <td>なし</td>
  </tr>
  <tr>
   <th scope="row">キャンセル</th>
   <td></td>
  </tr>
  <tr>
   <th scope="row">インターフェイス</th>
   <td>{{DOMxRef("Event")}}</td>
  </tr>
  <tr>
   <th scope="row">イベントハンドラープロパティ</th>
   <td>{{domxref("GlobalEventHandlers.oninvalid")}}</td>
  </tr>
 </tbody>
</table>

<p>このイベントは、送信時にフォームの問題の概要を表示するのに便利です。フォームが送信されると、 <code>invalid</code> イベントがそれぞれの妥当ではない状態にあるフォームコントロールで発生します。送信可能な要素が妥当であるかどうかは、その所有者である {{HtmlElement("form")}} を送信する前、またはその要素またはその所有者である <code>&lt;form&gt;</code><a href="/ja/docs/HTML/Forms_in_HTML#Constraint_Validation_API"><code>checkValidity()</code></a> メソッドが呼び出された後にチェックされます。</p>

<p>{{domxref("Element/blur_event", "blur")}} ではチェックが行われません。</p>

<h2 id="Examples" name="Examples"></h2>

<p>フォームが無効な値で送信された場合、送信可能な要素がチェックされ、エラーが見つかった場合、無効な要素で <code>invalid</code> イベントが発生します。この例では、入力に無効な値があったために <code>invalid</code> イベントが発生した場合、無効な値がログに記録されます。</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html notranslate">&lt;form action="#"&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;label&gt;Enter an integer between 1 and 10: &lt;input type="number" min="1" max="10" required&gt;&lt;/label&gt;&lt;/li&gt;
    &lt;li&gt;&lt;input type="submit" value="submit"&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/form&gt;&lt;p id="log"&gt;&lt;/p&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js notranslate">const input = document.querySelector('input')
const log = document.getElementById('log')

input.addEventListener('invalid', logValue)

function logValue(e) {
  log.textContent += e.target.value
}</pre>

<h3 id="Result" name="Result">結果</h3>

<p>{{EmbedLiveSample("Examples")}}</p>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
   <th scope="col">状態</th>
   <th scope="col">備考</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('HTML WHATWG', 'forms.html#the-constraint-validation-api', 'Invalid event') }}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{ SpecName('HTML5.1', 'sec-forms.html#the-constraint-validation-api', 'Invalid event') }}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{ SpecName('HTML5 W3C', 'forms.html#the-constraint-validation-api', 'Invalid event') }}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

<p>{{Compat("api.HTMLInputElement.invalid_event")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>HTML の {{HtmlElement("form")}} element</li>
 <li>関連イベント: {{domxref("HTMLFormElement/submit_event", "submit")}}</li>
 <li>CSS の {{cssxref(":invalid")}} 擬似クラス</li>
</ul>