aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/html/element/input/tel/index.html
blob: 07198f4c82d8ca9291e5866dd7a12913a7f01a42 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
---
title: <input type="tel">
slug: Web/HTML/Element/input/tel
tags:
  - Element
  - Form Inputs
  - Forms
  - HTML
  - HTML forms
  - HTML input
  - Input
  - Input Element
  - Input Type
  - Phone Numbers
  - Reference
  - Telephone Numbers
  - tel
translation_of: Web/HTML/Element/input/tel
---
<div>{{HTMLRef("Input_types")}}</div>

<p><span class="seoSummary">{{HTMLElement("input")}} 要素の <code><strong>tel</strong></code> 型は、ユーザーに電話番号を入力または編集させるために使用します。 <code><a href="/ja/docs/Web/HTML/Element/input/email">&lt;input type="email"&gt;</a></code><code><a href="/ja/docs/Web/HTML/Element/input/url">&lt;input type="url"&gt;</a></code> とは異なり、送信前に値が特定の書式であると自動的には検証されません。電話番号の書式は世界中で様々だからです。</span></p>

<div>{{EmbedInteractiveExample("pages/tabbed/input-tel.html", "tabbed-standard")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<p><code>tel</code> の入力欄は機能的に標準の <code>text</code> 入力欄と同じであるという事実にもかかわらず、便利な用途を提供します。もっとも手っ取り早く表れるのは、モバイルブラウザー — 特に携帯電話のもの — では、電話番号の入力に最適化された専用のキーパッドを表示することがあります。電話番号の専用の入力型を使用すると、カスタム検証を追加して電話番号の扱いをもっと便利にすることができます。</p>

<div class="note">
<p><strong></strong>: <code>tel</code> 型に対応していないブラウザーでは、通常の {{HTMLElement("input/text", "text")}} 入力欄で代用されます。</p>
</div>

<table class="properties">
 <tbody>
  <tr>
   <td><strong>{{anch("Value", "値")}}</strong></td>
   <td>電話番号を表す {{domxref("DOMString")}}、または空欄</td>
  </tr>
  <tr>
   <td><strong>イベント</strong></td>
   <td>{{domxref("HTMLElement/change_event", "change")}} および {{domxref("HTMLElement/input_event", "input")}}</td>
  </tr>
  <tr>
   <td><strong>対応している共通属性</strong></td>
   <td>{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("maxlength", "input")}}, {{htmlattrxref("minlength", "input")}}, {{htmlattrxref("pattern", "input")}}, {{htmlattrxref("placeholder", "input")}}, {{htmlattrxref("readonly", "input")}}, {{htmlattrxref("size", "input")}}</td>
  </tr>
  <tr>
   <td><strong>IDL 属性</strong></td>
   <td><code>list</code>, <code>selectionStart</code>, <code>selectionEnd</code>, <code>selectionDirection</code>, and <code>value</code></td>
  </tr>
  <tr>
   <td><strong>メソッド</strong></td>
   <td>{{domxref("HTMLInputElement.select", "select()")}}, {{domxref("HTMLInputElement.setRangeText", "setRangeText()")}}, {{domxref("HTMLInputElement.setSelectionRange", "setSelectionRange()")}}</td>
  </tr>
 </tbody>
</table>

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

<p>{{HTMLElement("input")}} 要素の {{htmlattrxref("value", "input")}} 属性には、電話番号を表す文字列、または空文字列 (<code>""</code>) の {{domxref("DOMString")}} が入ります。</p>

<h2 id="Additional_attributes" name="Additional_attributes">追加の属性</h2>

<p>型に関係なくすべての {{HTMLElement("input")}} 要素を操作する属性に加え、電話番号型の入力欄は次の属性にも対応しています。</p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">属性</th>
   <th scope="col">説明</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>{{anch("list")}}</code></td>
   <td>オプションで自動補完の定義済みの選択肢を含む &lt;datalist&gt; 要素の id</td>
  </tr>
  <tr>
   <td><code>{{anch("maxlength")}}</code></td>
   <td>妥当な入力として受け入れるための、 UTF-16 文字単位の最大長</td>
  </tr>
  <tr>
   <td><code>{{anch("minlength")}}</code></td>
   <td>フィールドの内容が妥当と判断される最小文字列長</td>
  </tr>
  <tr>
   <td><code>{{anch("pattern")}}</code></td>
   <td>制約検証に通るために入力された値が一致する必要がある正規表現</td>
  </tr>
  <tr>
   <td><code>{{anch("placeholder")}}</code></td>
   <td>値がない時にフィールド内に表示される入力例の値</td>
  </tr>
  <tr>
   <td><code>{{anch("readonly")}}</code></td>
   <td>論理属性で、存在すれば、フィールドの内容をユーザーが編集できないことを示す</td>
  </tr>
  <tr>
   <td><code>{{anch("size")}}</code></td>
   <td>画面上における入力フィールドの幅の文字数</td>
  </tr>
 </tbody>
</table>

<p id="htmlattrdeflist">{{page("/ja/docs/Web/HTML/Element/input/text", "list", 0, 1, 2)}}</p>

<h3 id="htmlattrdefmaxlength">{{htmlattrdef("maxlength")}}</h3>

<p>ユーザーが電話番号フィールドに入力することができる (UTF-16 コード単位での) 最大文字数です。 0 以上の整数値である必要があります。 <code>maxlength</code> が指定されていないか、無効な値が指定されていると、電話番号フィールドには最大文字数が設定されません。この値は <code>minlength</code> の値以上である必要もあります。</p>

<p>フィールドに入力された長さが UTF-16 コード単位で <code>maxlength</code> の長さを超えていると、その入力欄は<a href="/ja/docs/Web/Guide/HTML/HTML5/Constraint_validation">制約検証</a>に失敗します。</p>

<h3 id="htmlattrdefminlength">{{htmlattrdef("minlength")}}</h3>

<p>ユーザーが電話番号フィールドに入力することができる (UTF-16 コード単位での) 最小文字数です。これは非負の整数値で、 <code>maxlength</code> で指定された値以下である必要があります。 <code>minlength</code> が指定されていないか、無効な値が指定されていると、電話番号フィールドには最小文字数が設定されません。</p>

<p>入力欄のテキストの長さが UTF-16 コード単位で <code>minlength</code> の長さよりも短いと、電話番号フィールドは<a href="/ja/docs/Web/Guide/HTML/HTML5/Constraint_validation">制約検証</a>に失敗します。</p>

<h3 id="htmlattrdefpattern">{{htmlattrdef("pattern")}}</h3>

<p>{{page("/ja/docs/Web/HTML/Element/input/text", "pattern-include")}}</p>

<p>詳細と例については後述の{{anch("Pattern validation", "パターン検証")}}を参照してください。</p>

<p>{{page("/ja/docs/Web/HTML/Element/input/text", "placeholder", 0, 1, 2)}}</p>

<p>{{page("/ja/docs/Web/HTML/Element/input/text", "readonly", 0, 1, 2)}}</p>

<p>{{page("/ja/docs/Web/HTML/Element/input/text", "size", 0, 1, 2)}}</p>

<h2 id="Non-standard_attributes" name="Non-standard_attributes">標準外の属性</h2>

<p>ブラウザーによっては、以下の標準外の属性が利用できます。一般的な規則として、できれば使用することを避けてください。</p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">属性</th>
   <th scope="col">説明</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>{{anch("autocorrect")}}</code></td>
   <td>この入力フィールドの編集中にオートコレクトを許可するかどうか。 <strong>Safari のみ。</strong></td>
  </tr>
  <tr>
   <td><code>{{anch("mozactionhint")}}</code></td>
   <td>入力欄を編集している間、ユーザーが <kbd>Enter</kbd> または <kbd>Return</kbd> キーを押したときに実行されるアクションを示す文字列です。これは仮想キーボードのキーに適切なラベルを特定するために使用します。 <strong>Android 版 Firefox for のみ。</strong></td>
  </tr>
 </tbody>
</table>

<h3 id="htmlattrdefautocorrect_non-standard_inline">{{htmlattrdef("autocorrect")}} {{non-standard_inline}}</h3>

<p>{{page("/ja/docs/Web/HTML/Element/input/text", "autocorrect-include")}}</p>

<h3 id="htmlattrdefmozactionhint_non-standard_inline">{{htmlattrdef("mozactionhint")}} {{non-standard_inline}}</h3>

<p>{{page("/ja/docs/Web/HTML/Element/input/text", "mozactionhint-include")}}</p>

<h2 id="tel_入力欄の使用">tel 入力欄の使用</h2>

<p>ウェブにおいて、電話番号はとてもよく収集されるデータの種類です。例えば、何らかの会員登録や通信販売サイトで、商取引や緊急時連絡の目的でユーザーに電話番号を尋ねることが良くあります。一般的にどのように電話番号が入力されるかを考えれば、残念ながら「1つのサイズですべてに合う」ような解決策は現実的ではありません。</p>

<p>幸い、自分でサイトの要件を検討し、自分で適切なレベルの検証を実装することができます。詳しくは、以下の{{anch("Validation", "検証")}}をご覧ください。</p>

<h3 id="Custom_keyboards" name="Custom_keyboards">カスタムキーボード</h3>

<p>One of the main advantages of <code>&lt;input type="tel"&gt;</code> is that it causes mobile browsers to display a special keyboard for entering phone numbers. For example, here's what the keypads look like on a couple of devices.</p>

<table class="standard-table">
 <caption>Examples of custom keyboards on mobile devices.</caption>
 <thead>
  <tr>
   <th scope="col">Firefox for Android</th>
   <th scope="col">WebKit iOS (Safari/Chrome/Firefox)</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><img alt="Firefox for Android screen shot" src="https://mdn.mozillademos.org/files/15441/fx-android-tel.png" style="height: 640px; width: 360px;"></td>
   <td><img alt="Firefox for iOS screenshot" src="https://mdn.mozillademos.org/files/15603/iphone-tel-keyboard-50pct.png" style="height: 640px; width: 360px;"></td>
  </tr>
 </tbody>
</table>

<h3 id="A_simple_tel_input" name="A_simple_tel_input">単純な tel 入力欄</h3>

<p>In its most basic form, a tel input can be implemented like this:</p>

<pre class="brush: html notranslate">&lt;label for="telNo"&gt;Phone number:&lt;/label&gt;
&lt;input id="telNo" name="telNo" type="tel"&gt;</pre>

<p>{{ EmbedLiveSample('A_simple_tel_input', 600, 40) }}</p>

<p>There is nothing magical going on here. When submitted to the server, the above input's data would be represented as, for example, <code>telNo=+12125553151</code>.</p>

<h3 id="Placeholders" name="Placeholders">プレイスホルダー</h3>

<p>Sometimes it's helpful to offer an in-context hint as to what form the input data should take. This can be especially important if the page design doesn't offer descriptive labels for each {{HTMLElement("input")}}. This is where <strong>placeholders</strong> come in. A placeholder is a value that demonstrates the form the <code>value</code> should take by presenting an example of a valid value, which is displayed inside the edit box when the element's <code>value</code> is <code>""</code>. Once data is entered into the box, the placeholder disappears; if the box is emptied, the placeholder reappears.</p>

<p>Here, we have an <code>tel</code> input with the placeholder <code>123-4567-8901</code>. Note how the placeholder disappears and reappears as you manipulate the contents of the edit field.</p>

<pre class="brush: html notranslate">&lt;input id="telNo" name="telNo" type="tel"
       placeholder="123-4567-8901"&gt;</pre>

<p>{{ EmbedLiveSample('Placeholders', 600, 40) }}</p>

<h3 id="Controlling_the_input_size" name="Controlling_the_input_size">入力欄の寸法の制御</h3>

<p>You can control not only the physical length of the input box, but also the minimum and maximum lengths allowed for the input text itself.</p>

<h4 id="Physical_input_element_size" name="Physical_input_element_size">物理的な入力欄の寸法</h4>

<p>The physical size of the input box can be controlled using the {{htmlattrxref("size", "input")}} attribute. With it, you can specify the number of characters the input box can display at a time. In this example, for instance, the <code>tel</code> edit box is 20 characters wide:</p>

<pre class="brush: html notranslate">&lt;input id="telNo" name="telNo" type="tel"
       size="20"&gt;</pre>

<p>{{ EmbedLiveSample('Physical_input_element_size', 600, 40) }}</p>

<h4 id="Element_value_length" name="Element_value_length">要素の値の長さ</h4>

<p>The <code>size</code> is separate from the length limitation on the entered telephone number. You can specify a minimum length, in characters, for the entered telephone number using the {{htmlattrxref("minlength", "input")}} attribute; similarly, use {{htmlattrxref("maxlength", "input")}} to set the maximum length of the entered telephone number.</p>

<p>The example below creates a 20-character wide telephone number entry box, requiring that the contents be no shorter than 9 characters and no longer than 14 characters.</p>

<pre class="brush: html notranslate">&lt;input id="telNo" name="telNo" type="tel"
       size="20" minlength="9" maxlength="14"&gt;</pre>

<p>{{EmbedLiveSample("Element_value_length", 600, 40) }}</p>

<div class="note">
<p><strong>Note</strong>: The above attributes do affect {{anch("Validation")}} — the above example's inputs will count as invalid if the length of the value is less than 9 characters, or more than 14. Most browser won't even let you enter a value over the max length.</p>
</div>

<h3 id="Providing_default_options" name="Providing_default_options">既定のオプションの提供</h3>

<p>これまでどおり、 {{htmlattrxref("value", "input")}} 属性を設定することで <code>tel</code> 入力欄に既定値を指定することができます。</p>

<div id="Default_value">
<pre class="brush: html notranslate">&lt;input id="telNo" name="telNo" type="tel"
       value="333-4444-4444"&gt;</pre>
</div>

<p>{{EmbedLiveSample("Default_value", 600, 40)}}</p>

<h4 id="Offering_suggested_values" name="Offering_suggested_values">サジェスト値の提供</h4>

<p>Taking it a step farther, you can provide a list of default phone number values from which the user can select. To do this, use the {{htmlattrxref("list", "input")}} attribute. This doesn't limit the user to those options, but does allow them to select commonly-used telephone numbers more quickly. This also offers hints to {{htmlattrxref("autocomplete", "input")}}. The <code>list</code> attribute specifies the ID of a {{HTMLElement("datalist")}} element, which in turn contains one {{HTMLElement("option")}} element per suggested value; each <code>option</code>'s <code>value</code> is the corresponding suggested value for the telephone number entry box.</p>

<pre class="brush: html notranslate">&lt;label for="telNo"&gt;Phone number: &lt;/label&gt;
&lt;input id="telNo" name="telNo" type="tel" list="defaultTels"&gt;

&lt;datalist id="defaultTels"&gt;
  &lt;option value="111-1111-1111"&gt;
  &lt;option value="122-2222-2222"&gt;
  &lt;option value="333-3333-3333"&gt;
  &lt;option value="344-4444-4444"&gt;
&lt;/datalist&gt;</pre>

<p>{{EmbedLiveSample("Offering_suggested_values", 600, 40)}}</p>

<p>With the {{HTMLElement("datalist")}} element and its {{HTMLElement("option")}}s in place, the browser will offer the specified values as potential values for the email address; this is typically presented as a popup or drop-down menu containing the suggestions. While the specific user experience may vary from one browser to another, typically clicking in the edit box presents a drop-down of the suggested email addresses. Then, as the user types, the list is adjusted to show only filtered matching values. Each typed character narrows down the list until the user makes a selection or types a custom value.</p>

<p>Here's a screenshot of what that might look like:</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/15605/phone-number-with-options.png" style="border-style: solid; border-width: 1px; height: 150px; width: 684px;"></p>

<h2 id="Validation" name="Validation">検証</h2>

<p>As we've touched on before, it's quite difficult to provide a one-size-fits-all client-side validation solution for phone numbers. So what can we do? Let's consider some options.</p>

<div class="warning">
<p><strong>重要</strong>: HTML のフォーム検証は、入力されたデータが正しい形式であることを保証するスクリプトの代用には<em>なりません</em>。 HTML を調整して検証をくぐり抜けたり、完全に削除したりすることはとても簡単にできます。 HTML を完全にバイパスし、サーバーに直接データを送信することも可能です。サーバー側のコードが受信したデータの検証に失敗した場合、不適切な形式のデータ (または大きすぎるデータ、間違った種類のデータなど) が送信された場合に災害が発生するおそれがあります。</p>
</div>

<h3 id="Making_telephone_numbers_required" name="Making_telephone_numbers_required">電話番号を必須にする</h3>

<p>You can make it so that an empty input is invalid and won't be submitted to the server using the {{htmlattrxref("required", "input")}} attribute. For example, let's use this HTML:</p>

<pre class="brush: html notranslate">&lt;form&gt;
  &lt;div&gt;
    &lt;label for="telNo"&gt;Enter a telephone number (required): &lt;/label&gt;
    &lt;input id="telNo" name="telNo" type="tel" required&gt;
    &lt;span class="validity"&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;button&gt;Submit&lt;/button&gt;
  &lt;/div&gt;
&lt;/form&gt;</pre>

<p>And let's include the following CSS to highlight valid entries with a checkmark and invalid entries with a cross:</p>

<pre class="brush: css notranslate">div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid+span:after {
  position: absolute; content: '✖';
  padding-left: 5px;
  color: #8b0000;
}

input:valid+span:after {
  position: absolute;
  content: '✓';
  padding-left: 5px;
  color: #009000;
}</pre>

<p>The output looks like this:</p>

<p>{{EmbedLiveSample("Making_telephone_numbers_required", 700, 70)}}</p>

<h3 id="Pattern_validation" name="Pattern_validation">パターンによる値検証</h3>

<p>If you want to further restrict entered numbers so they also have to conform to a specific pattern, you can use the {{htmlattrxref("pattern","input")}} attribute, which takes as its value a {{Glossary("regular expression")}} that entered values have to match.</p>

<p>In this example we'll use the same CSS as before, but our HTML is changed to look like this:</p>

<pre class="brush: html notranslate">&lt;form&gt;
  &lt;div&gt;
    &lt;label for="telNo"&gt;Enter a telephone number (in the form xxx-xxx-xxxx): &lt;/label&gt;
    &lt;input id="telNo" name="telNo" type="tel" required
           pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"&gt;
    &lt;span class="validity"&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;button&gt;Submit&lt;/button&gt;
  &lt;/div&gt;
&lt;/form&gt;</pre>

<div class="hidden">
<pre class="brush: css notranslate">div {
  margin-bottom: 10px;
  position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid+span:after {
  position: absolute; content: '✖';
  padding-left: 5px;
  color: #8b0000;
}

input:valid+span:after {
  position: absolute;
  content: '✓';
  padding-left: 5px;
  color: #009000;
}</pre>
</div>

<p>{{EmbedLiveSample("Pattern_validation", 700, 70)}}</p>

<p>Notice how the entered value is reported as invalid unless the pattern xxx-xxx-xxxx is matched; for instance, 41-323-421 won't be accepted. Neither will 800-MDN-ROCKS. However, 865-555-6502 will be accepted. This particular pattern is obviously only useful for certain locales — in a real application you'd probably have to vary the pattern used depending on the locale of the user.</p>

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

<p>In this example, we present a simple interface with a {{htmlelement("select")}} element that lets the user choose which country they're in, and a set of <code>&lt;input type="tel"&gt;</code> elements to let them enter each part of their phone number; there is no reason why you can't have multiple <code>tel</code> inputs.</p>

<p>Each input has a {{htmlattrxref("placeholder","input")}} attribute to show a hint to sighted users about what to enter into it, a {{htmlattrxref("pattern","input")}} to enforce a specific number of characters for the desired section, and an <code>aria-label</code> attribute to contain a hint to be read out to screenreader users about what to enter into it.</p>

<pre class="brush: html notranslate">&lt;form&gt;
  &lt;div&gt;
    &lt;label for="country"&gt;Choose your country:&lt;/label&gt;
    &lt;select id="country" name="country"&gt;
      &lt;option&gt;UK&lt;/option&gt;
      &lt;option selected&gt;US&lt;/option&gt;
      &lt;option&gt;Germany&lt;/option&gt;
    &lt;/select&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;p&gt;Enter your telephone number: &lt;/p&gt;
    &lt;span class="areaDiv"&gt;
      &lt;input id="areaNo" name="areaNo" type="tel" required
             placeholder="Area code" pattern="[0-9]{3}"
             aria-label="Area code"&gt;
      &lt;span class="validity"&gt;&lt;/span&gt;
    &lt;/span&gt;
    &lt;span class="number1Div"&gt;
      &lt;input id="number1" name="number1" type="tel" required
             placeholder="First part" pattern="[0-9]{3}"
             aria-label="First part of number"&gt;
      &lt;span class="validity"&gt;&lt;/span&gt;
    &lt;/span&gt;
    &lt;span class="number2Div"&gt;
      &lt;input id="number2" name="number2" type="tel" required
             placeholder="Second part" pattern="[0-9]{4}"
             aria-label="Second part of number"&gt;
      &lt;span class="validity"&gt;&lt;/span&gt;
    &lt;/span&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;button&gt;Submit&lt;/button&gt;
  &lt;/div&gt;
&lt;/form&gt;</pre>

<p>The JavaScript is relatively simple — it contains an {{domxref("GlobalEventHandlers.onchange", "onchange")}} event handler that, when the <code>&lt;select&gt;</code> value is changed, updates the <code>&lt;input&gt;</code> element's <code>pattern</code>, <code>placeholder</code>, and <code>aria-label</code> to suit the format of telephone numbers in that country/territory.</p>

<pre class="brush: js notranslate">var selectElem = document.querySelector("select");
var inputElems = document.querySelectorAll("input");

selectElem.onchange = function() {
  for(var i = 0; i &lt; inputElems.length; i++) {
    inputElems[i].value = "";
  }

  if(selectElem.value === "US") {
    inputElems[2].parentNode.style.display = "inline";

    inputElems[0].placeholder = "Area code";
    inputElems[0].pattern = "[0-9]{3}";

    inputElems[1].placeholder = "First part";
    inputElems[1].pattern = "[0-9]{3}";
    inputElems[1].setAttribute("aria-label","First part of number");

    inputElems[2].placeholder = "Second part";
    inputElems[2].pattern = "[0-9]{4}";
    inputElems[2].setAttribute("aria-label","Second part of number");
  } else if(selectElem.value === "UK") {
    inputElems[2].parentNode.style.display = "none";

    inputElems[0].placeholder = "Area code";
    inputElems[0].pattern = "[0-9]{3,6}";

    inputElems[1].placeholder = "Local number";
    inputElems[1].pattern = "[0-9]{4,8}";
    inputElems[1].setAttribute("aria-label","Local number");
  } else if(selectElem.value === "Germany") {
    inputElems[2].parentNode.style.display = "inline";

    inputElems[0].placeholder = "Area code";
    inputElems[0].pattern = "[0-9]{3,5}";

    inputElems[1].placeholder = "First part";
    inputElems[1].pattern = "[0-9]{2,4}";
    inputElems[1].setAttribute("aria-label","First part of number");

    inputElems[2].placeholder = "Second part";
    inputElems[2].pattern = "[0-9]{4}";
    inputElems[2].setAttribute("aria-label","Second part of number");
  }
}</pre>

<p>The example looks like this:</p>

<p>{{EmbedLiveSample('Examples', 600, 140)}}</p>

<p>This is an interesting idea, which goes to show a potential solution to the problem of dealing with international phone numbers. You would have to extend the example of course to provide the correct pattern for potentially every country, which would be a lot of work, and there would still be no foolproof guarantee that the users would enter their numbers correctly.</p>

<p>It makes you wonder if it is worth going to all this trouble on the client-side, when you could just let the user enter their number in whatever format they wanted on the client-side and then validate and sanitize it on the server. But this choice is yours to make.</p>

<div class="hidden">
<pre class="brush: css notranslate">div {
margin-bottom: 10px;
position: relative;
}

input[type="number"] {
  width: 100px;
}

input + span {
  padding-right: 30px;
}

input:invalid+span:after {
  position: absolute; content: '✖';
  padding-left: 5px;
  color: #8b0000;
}

input:valid+span:after {
  position: absolute;
  content: '✓';
  padding-left: 5px;
  color: #009000;
}</pre>
</div>

<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', '#telephone-state-(type=tel)', '&lt;input type="tel"&gt;')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>初回定義</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', 'sec-forms.html#tel-state-typetel', '&lt;input type="tel"&gt;')}}</td>
   <td>{{Spec2('HTML5.1')}}</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("html.elements.input.input-tel")}}</p>

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

<ul>
 <li><a href="/ja/docs/Learn/HTML/Forms">HTML フォームガイド</a></li>
 <li><a href="/ja/docs/Web/Accessibility/ARIA/forms">フォームとアクセシビリティ</a></li>
 <li>{{HTMLElement("input")}}
  <ul>
   <li><code><a href="/ja/docs/Web/HTML/Element/input/text">&lt;input type="text"&gt;</a></code></li>
   <li><code><a href="/ja/docs/Web/HTML/Element/input/email">&lt;input type="email"&gt;</a></code></li>
  </ul>
 </li>
 <li><a href="/ja/docs/Learn/Forms/Property_compatibility_table_for_form_controls">CSS プロパティの互換性</a></li>
</ul>