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
|
---
title: <input type="search">
slug: Web/HTML/Element/Input/search
translation_of: Web/HTML/Element/input/search
---
<div>{{HTMLRef}}</div>
<p><span class="seoSummary">{{HTMLElement("input")}} элементы с типом <code><strong>search</strong></code> это текстовые поля разработанные для ввода пользователем поисковых запросов. Функциональность идентична элементам с типом <code><a href="/en-US/docs/Web/HTML/Element/input/text">text</a></code>, но может отличаться стилизация за счёт {{Glossary("user agent")}}, а также наличием специального <strong>не стандартизированного</strong> события </span>{{domxref("HTMLElement/search_event", "onsearch")}}.</p>
<div>{{EmbedInteractiveExample("pages/tabbed/input-search.html", "tabbed-standard")}}</div>
<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>
<table class="properties">
<tbody>
<tr>
<td><strong>{{anch("Value")}}</strong></td>
<td>A {{domxref("DOMString")}} representing the value contained in the search field.</td>
</tr>
<tr>
<td><strong>Events</strong></td>
<td>{{domxref("HTMLElement/change_event", "change")}} and {{domxref("HTMLElement/input_event", "input")}}</td>
</tr>
<tr>
<td><strong>Supported Common Attributes</strong></td>
<td>{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("maxlength", "input")}}, {{htmlattrxref("minlength", "input")}}, {{htmlattrxref("pattern", "input")}}, {{htmlattrxref("placeholder", "input")}}, {{htmlattrxref("required", "input")}}, {{htmlattrxref("size", "input")}}.</td>
</tr>
<tr>
<td><strong>IDL attributes</strong></td>
<td><code>value</code></td>
</tr>
<tr>
<td><strong>Methods</strong></td>
<td>{{domxref("HTMLInputElement.select", "select()")}}, {{domxref("HTMLInputElement.setRangeText", "setRangeText()")}}, {{domxref("HTMLInputElement.setSelectionRange", "setSelectionRange()")}}.</td>
</tr>
</tbody>
</table>
<h2 id="Value">Value</h2>
<p>The {{htmlattrxref("value", "input")}} attribute contains a {{domxref("DOMString")}} representing the value contained in the search field. You can retrieve this using the {{domxref("HTMLInputElement.value")}} property in JavaScript.</p>
<pre class="brush: js">searchTerms = mySearch.value;
</pre>
<p>If no validation constraints are in place for the input (see {{anch("Validation")}} for more details), the value can be any text string or an empty string (<code>""</code>).</p>
<h2 id="Additional_attributes">Additional attributes</h2>
<p>In addition to the attributes that operate on all {{HTMLElement("input")}} elements regardless of their type, search field inputs support the following attributes:</p>
<table>
<thead>
<tr>
<th scope="col">Attribute</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>{{anch("list")}}</code></td>
<td>The id of the <datalist> element that contains the optional pre-defined autocomplete options</td>
</tr>
<tr>
<td><code>{{anch("maxlength")}}</code></td>
<td>The maximum number of characters the input should accept</td>
</tr>
<tr>
<td><code>{{anch("minlength")}}</code></td>
<td>The minimum number of characters long the input can be and still be considered valid</td>
</tr>
<tr>
<td><code>{{anch("pattern")}}</code></td>
<td>A regular expression the input's contents must match in order to be valid</td>
</tr>
<tr>
<td><code>{{anch("placeholder")}}</code></td>
<td>An exemplar value to display in the input field whenever it is empty</td>
</tr>
<tr>
<td><code>{{anch("readonly")}}</code></td>
<td>A Boolean attribute indicating whether or not the contents of the input should be read-only</td>
</tr>
<tr>
<td><code>{{anch("size")}}</code></td>
<td>A number indicating how many characters wide the input field should be</td>
</tr>
<tr>
<td><code>{{anch("spellcheck")}}</code></td>
<td>Controls whether or not to enable spell checking for the input field, or if the default spell checking configuration should be used</td>
</tr>
</tbody>
</table>
<p id="htmlattrdeflist">{{page("/en-US/docs/Web/HTML/Element/input/text", "list", 0, 1, 2)}}</p>
<h3 id="htmlattrdefmaxlength">{{htmlattrdef("maxlength")}}</h3>
<p>The maximum number of characters (as UTF-16 code units) the user can enter into the search field. This must be an integer value 0 or higher. If no <code>maxlength</code> is specified, or an invalid value is specified, the search field has no maximum length. This value must also be greater than or equal to the value of <code>minlength</code>.</p>
<p>The input will fail <a href="/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation">constraint validation</a> if the length of the text entered into the field is greater than <code>maxlength</code> UTF-16 code units long.</p>
<h3 id="htmlattrdefminlength">{{htmlattrdef("minlength")}}</h3>
<p>The minimum number of characters (as UTF-16 code units) the user can enter into the search field. This must be a non-negative integer value smaller than or equal to the value specified by <code>maxlength</code>. If no <code>minlength</code> is specified, or an invalid value is specified, the search input has no minimum length.</p>
<p>The search field will fail <a href="/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation">constraint validation</a> if the length of the text entered into the field is fewer than <code>minlength</code> UTF-16 code units long.</p>
<h3 id="htmlattrdefpattern">{{htmlattrdef("pattern")}}</h3>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "pattern-include")}}</p>
<p>See the section {{anch("Specifying a pattern")}} for details and an example.</p>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "placeholder", 0, 1, 2)}}</p>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "readonly", 0, 1, 2)}}</p>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "size", 0, 1, 2)}}</p>
<h3 id="htmlattrdefspellcheck">{{htmlattrdef("spellcheck")}}</h3>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "spellcheck-include")}}</p>
<h2 id="Non-standard_attributes">Non-standard attributes</h2>
<p>The following non-standard attributes are available to search input fields. As a general rule, you should avoid using them unless it can't be helped.</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Attribute</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>{{anch("autocorrect")}}</code></td>
<td>Whether or not to allow autocorrect while editing this input field. <strong>Safari only.</strong></td>
</tr>
<tr>
<td><code>{{anch("incremental")}}</code></td>
<td>Whether or not to send repeated {{event("search")}} events to allow updating live search results while the user is still editing the value of the field. <strong>WebKit and Blink only (Safari, Chrome, Opera, etc.).</strong></td>
</tr>
<tr>
<td><code>{{anch("mozactionhint")}}</code></td>
<td>A string indicating the type of action that will be taken when the user presses the <kbd>Enter</kbd> or <kbd>Return</kbd> key while editing the field; this is used to determine an appropriate label for that key on a virtual keyboard. <strong>Firefox for Android only.</strong></td>
</tr>
<tr>
<td><code>{{anch("results")}}</code></td>
<td>The maximum number of items that should be displayed in the drop-down list of previous search queries. <strong>Safari only.</strong></td>
</tr>
</tbody>
</table>
<h3 id="htmlattrdefautocorrect_non-standard_inline">{{htmlattrdef("autocorrect")}} {{non-standard_inline}}</h3>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "autocorrect-include")}}</p>
<h3 id="htmlattrdefincremental_non-standard_inline">{{htmlattrdef("incremental")}} {{non-standard_inline}}</h3>
<div id="incremental-include">
<p>The Boolean attribute <code>incremental</code> is a WebKit and Blink extension (so supported by Safari, Opera, Chrome, etc.) which, if present, tells the {{Glossary("user agent")}} to process the input as a live search. As the user edits the value of the field, the user agent sends {{event("search")}} events to the {{domxref("HTMLInputElement")}} object representing the search box. This allows your code to update the search results in real time as the user edits the search.</p>
<p>If <code>incremental</code> is not specified, the {{event("search")}} event is only sent when the user explicitly initiates a search (such as by pressing the <kbd>Enter</kbd> or <kbd>Return</kbd> key while editing the field).</p>
<p>The <code>search</code> event is rate-limited so that it is not sent more more frequently than an implementation-defined interval.</p>
</div>
<h3 id="htmlattrdefmozactionhint_non-standard_inline">{{htmlattrdef("mozactionhint")}} {{non-standard_inline}}</h3>
<p>{{page("/en-US/docs/Web/HTML/Element/input/text", "mozactionhint-include")}}</p>
<h3 id="htmlattrdefresults_non-standard_inline">{{htmlattrdef("results")}} {{non-standard_inline}}</h3>
<div id="results-include">
<p>The <code>results</code> attribute—supported only by Safari—is a numeric value that lets you override the maximum number of entries to be displayed in the {{HTMLElement("input")}} element's natively-provided drop-down menu of previous search queries.</p>
<p>The value must be a non-negative decimal number. If not provided, or an invalid value is given, the browser's default maximum number of entries is used.</p>
</div>
<h2 id="Using_search_inputs">Using search inputs</h2>
<p><code><input></code> elements of type <code>search</code> are very similar to those of type <code>text</code>, except that they are specifically intended for handling search terms. They are basically equivalent in behavior, but user agents may choose to style them differently by default (and, of course, sites may use stylesheets to apply custom styles to them).</p>
<h3 id="Basic_example">Basic example</h3>
<pre class="brush: html"><form>
<div>
<input type="search" id="mySearch" name="q">
<button>Search</button>
</div>
</form></pre>
<p>This renders like so:</p>
<p>{{EmbedLiveSample("Basic_example", 600, 40)}}</p>
<p><code>q</code> is the most common <code>name</code> given to search inputs, although it's not mandatory. When submitted, the data name/value pair sent to the server will be <code>q=searchterm</code>.</p>
<div class="note">
<p>You must remember to set a {{htmlattrxref("name", "input")}} for your input, otherwise nothing will be submitted.</p>
</div>
<h3 id="Differences_between_search_and_text_types">Differences between search and text types</h3>
<p>The main basic differences come in the way browsers handle them. The first thing to note is that some browsers show a cross icon that can be clicked on to remove the search term instantly if desired. The following screenshot comes from Chrome:</p>
<p><img alt="" src="https://mdn.mozillademos.org/files/15235/chrome-cross-icon.png" style="display: block; height: 31px; margin: 0px auto; width: 144px;"></p>
<p>In addition, modern browsers also tend to automatically store search terms previously entered across domains, which then come up as autocomplete options when subsequent searches are performed in search inputs on that domain. This helps users who tend to do searches on the same or similar search queries over time. This screenshot is from Firefox:</p>
<p><img alt="" src="https://mdn.mozillademos.org/files/15237/firefox-auto-complete.png" style="display: block; height: 83px; margin: 0px auto; width: 171px;">At this point, let's look at some useful techniques you can apply to your search forms.</p>
<h3 id="Setting_placeholders">Setting placeholders</h3>
<p>You can provide a useful placeholder inside your search input that could give a hint on what to do using the {{htmlattrxref("placeholder","input")}} attribute. Look at the following example:</p>
<pre class="brush: html"><form>
<div>
<input type="search" id="mySearch" name="q"
placeholder="Search the site...">
<button>Search</button>
</div>
</form></pre>
<p>You can see how the placeholder is rendered below:</p>
<p>{{EmbedLiveSample("Setting_placeholders", 600, 40)}}</p>
<h3 id="Search_form_labels_and_accessibility">Search form labels and accessibility</h3>
<p>One problem with search forms is their accessibility; a common design practice is not to provide a label for the search field (although there might be a magnifying glass icon or similar), as the purpose of a search form is normally fairly obvious for sighted users due to placement (<a href="https://mdn.github.io/learning-area/accessibility/aria/website-aria-roles/">this example shows a typical pattern</a>).</p>
<p>This could, however, cause confusion for screenreader users, since they will not have any verbal indication of what the search input is. One way around this that won't impact on your visual design is to use <a href="/en-US/docs/Learn/Accessibility/WAI-ARIA_basics">WAI-ARIA</a> features:</p>
<ul>
<li>A <code>role</code> attribute of value <code>search</code> on the <code><form></code> element will cause screenreaders to announce that the form is a search form.</li>
<li>If that isn't enough, you can use an <code>aria-label</code> attribute on the {{HTMLElement("input")}} itself. This should be a descriptive text label that will be read out by the screenreader; it's used as a non-visual equivalent to <code><label></code>.</li>
</ul>
<p>Let's have a look at an example:</p>
<pre class="brush: html"><form role="search">
<div>
<input type="search" id="mySearch" name="q"
placeholder="Search the site..."
aria-label="Search through site content">
<button>Search</button>
</div>
</form></pre>
<p>You can see how this is rendered below:</p>
<p>{{EmbedLiveSample("Search_form_labels_and_accessibility", 600, 40)}}</p>
<p>There is no visual difference from the previous example, but screenreader users have way more information available to them.</p>
<div class="note">
<p><strong>Note</strong>: See <a href="/en-US/docs/Learn/Accessibility/WAI-ARIA_basics#SignpostsLandmarks">Signposts/Landmarks</a> for more information about such accessibility features.</p>
</div>
<h3 id="Physical_input_element_size">Physical input element size</h3>
<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 search box is 30 characters wide:</p>
<pre class="brush: html"><form>
<div>
<input type="search" id="mySearch" name="q"
placeholder="Search the site..." size="30">
<button>Search</button>
</div>
</form></pre>
<p>The result is this wider input box:</p>
<p>{{ EmbedLiveSample('Physical_input_element_size', 600, 40) }}</p>
<h2 id="Validation">Validation</h2>
<p><code><input></code> elements of type <code>search</code> have the same validation features available to them as regular <code>text</code> inputs. It is less likely that you'd want to use validation features in general for search boxes. In many cases, users should just be allowed to search for anything, but there are a few cases to consider, such as searches against data of a known format.</p>
<div class="note">
<p><strong>Note</strong>: HTML form validation is <em>not</em> a substitute for scripts that ensure that the entered data is in the proper format. It's far too easy for someone to make adjustments to the HTML that allow them to bypass the validation, or to remove it entirely. It's also possible for someone to simply bypass your HTML entirely and submit the data directly to your server. If your server-side code fails to validate the data it receives, disaster could strike when improperly-formatted data (or data which is too large, is of the wrong type, and so forth) is entered into your database.</p>
</div>
<h3 id="A_note_on_styling">A note on styling</h3>
<p>There are useful pseudo-classes available for styling valid/invalid form elements: {{cssxref(":valid")}} and {{cssxref(":invalid")}}. In this section, we'll use the following CSS, which will place a check (tick) next to inputs containing valid values, and a cross next to inputs containing invalid values.</p>
<pre class="brush: css">input:invalid ~ span:after {
content: '✖';
padding-left: 5px;
position: absolute;
}
input:valid ~ span:after {
content: '✓';
padding-left: 5px;
position: absolute;
}</pre>
<p>The technique also requires a {{htmlelement("span")}} element to be placed after the form element, which acts as a holder for the icons. This was necessary because some input types on some browsers don't display icons placed directly after them very well.</p>
<h3 id="Making_input_required">Making input required</h3>
<p>You can use the {{htmlattrxref("required", "input")}} attribute as an easy way of making entering a value required before form submission is allowed:</p>
<pre class="brush: html"><form>
<div>
<input type="search" id="mySearch" name="q"
placeholder="Search the site..." required>
<button>Search</button>
<span class="validity"></span>
</div>
</form></pre>
<div class="hidden">
<pre class="brush: css">input {
margin-right: 10px;
}
input:invalid ~ span:after {
content: '✖';
padding-left: 5px;
position: absolute;
}
input:valid ~ span:after {
content: '✓';
padding-left: 5px;
position: absolute;
}</pre>
</div>
<p>This renders like so:</p>
<p>{{ EmbedLiveSample('Making_input_required', 600, 40) }}</p>
<p>In addition, if you try to submit the form with no search term entered into it, the browser will show a message. The following example is from Firefox:</p>
<p><img alt="form field with attached message that says Please fill out this field" src="https://mdn.mozillademos.org/files/15241/firefox-required-message.png" style="display: block; margin: 0 auto;"></p>
<p>Different messages will be shown when you try to submit the form with different types of invalid data contained inside the inputs; see the below examples.</p>
<h3 id="Input_value_length">Input value length</h3>
<p>You can specify a minimum length, in characters, for the entered value using the {{htmlattrxref("minlength", "input")}} attribute; similarly, use {{htmlattrxref("maxlength", "input")}} to set the maximum length of the entered value.</p>
<p>The example below requires that the entered value be 4–8 characters in length.</p>
<pre class="brush: html"><form>
<div>
<label for="mySearch">Search for user</label>
<input type="search" id="mySearch" name="q"
placeholder="User IDs are 4–8 characters in length" required
size="30" minlength="4" maxlength="8">
<button>Search</button>
<span class="validity"></span>
</div>
</form></pre>
<div class="hidden">
<pre class="brush: css">input {
margin-right: 10px;
}
input:invalid ~ span:after {
content: '✖';
padding-left: 5px;
position: absolute;
}
input:valid ~ span:after {
content: '✓';
padding-left: 5px;
position: absolute;
}</pre>
</div>
<p>This renders like so:</p>
<p>{{ EmbedLiveSample('Input_value_length', 600, 40) }}</p>
<p>If you try to submit the form with less than 4 characters, you'll be given an appropriate error message (which differs between browsers). If you try to go beyond 8 characters in length, the browser won't let you.</p>
<h3 id="Specifying_a_pattern">Specifying a pattern</h3>
<p>You can use the {{htmlattrxref("pattern", "input")}} attribute to specify a regular expression that the inputted value must follow to be considered valid (see <a href="/en-US/docs/Learn/HTML/Forms/Form_validation#Validating_against_a_regular_expression">Validating against a regular expression</a> for a simple crash course).</p>
<p>Let's look at an example. Say we wanted to provide a product ID search form, and the IDs were all codes of two letters followed by four numbers. The following example covers it:</p>
<pre class="brush: html"><form>
<div>
<label for="mySearch">Search for product by ID:</label>
<input type="search" id="mySearch" name="q"
placeholder="two letters followed by four numbers" required
size="30" pattern="[A-z]{2}[0-9]{4}">
<button>Search</button>
<span class="validity"></span>
</div>
</form></pre>
<div class="hidden">
<pre class="brush: css">input {
margin-right: 10px;
}
input:invalid ~ span:after {
content: '✖';
padding-left: 5px;
position: absolute;
}
input:valid ~ span:after {
content: '✓';
padding-left: 5px;
position: absolute;
}</pre>
</div>
<p>This renders like so:</p>
<p>{{ EmbedLiveSample('Specifying_a_pattern', 600, 40) }}</p>
<h2 id="Examples">Examples</h2>
<p>You can see a good example of a search form used in context at our <a class="external external-icon" href="https://github.com/mdn/learning-area/tree/master/accessibility/aria/website-aria-roles">website-aria-roles</a> example (<a class="external external-icon" href="http://mdn.github.io/learning-area/accessibility/aria/website-aria-roles/">see it live</a>).</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('HTML WHATWG', 'input.html#text-(type=text)-state-and-search-state-(type=search)', '<input type="search">')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td>Initial definition</td>
</tr>
<tr>
<td>{{SpecName('HTML5.1', 'sec-forms.html#text-typetext-state-and-search-state-typesearch', '<input type="search">')}}</td>
<td>{{Spec2('HTML5.1')}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Learn/HTML/Forms">HTML Forms</a></li>
<li>{{HTMLElement("input")}} and the {{domxref("HTMLInputElement")}} interface it's based upon</li>
<li><code><a href="/en-US/docs/Web/HTML/Element/input/text"><input type="text"></a></code></li>
<li><a href="/en-US/docs/Learn/HTML/Forms/Property_compatibility_table_for_form_widgets">Compatibility of CSS properties</a></li>
</ul>
|