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
|
---
title: <input type="email">
slug: Web/HTML/Element/Input/email
tags:
- HTML
- HTML forms
- Input Type
- 参考
- 电子邮件
- 表单
translation_of: Web/HTML/Element/input/email
---
<div>{{HTMLRef}}</div>
<p><span class="seoSummary">带有 <code><strong>"email"</strong></code> (电子邮箱) 类型标记的输入框元素 ({{HTMLElement("input")}}) 能够让用户输入或编辑一个电子邮箱地址,此外,如果指定了{{htmlattrxref("multiple", "input")}}属性,用户还可以输入多个电子邮箱地址。在表单提交前,输入框会自动验证输入值是否是一个或多个合法的电子邮箱地址 (非空值且符合电子邮箱地址格式).</span> CSS 伪标签 {{cssxref(":valid")}} 和 {{cssxref(":invalid")}} 能够在校验后自动应用。</p>
<div class="note">
<p><strong>注意</strong>: 不支持 “电子邮件” 类型的浏览器可以回退为标准的 “文本” 输入。</p>
</div>
<div>{{EmbedInteractiveExample("pages/tabbed/input-email.html", "tabbed-standard")}}</div>
<p 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>">https://github.com/mdn/interactive-examples </a> and send us a pull request.</p>
<table class="properties">
<tbody>
<tr>
<td><strong><a href="#值">值</a></strong></td>
<td>一个邮箱地址格式的{{domxref("DOMString")}} 或空值</td>
</tr>
<tr>
<td><strong>事件</strong></td>
<td>{{event("change")}} 和 {{event("input")}}</td>
</tr>
<tr>
<td>
<p><strong>支持的属性</strong></p>
<p><strong>通用的属性</strong></p>
</td>
<td>{{htmlattrxref("autocomplete", "input")}}, {{htmlattrxref("list", "input")}}, {{htmlattrxref("maxlength", "input")}}, {{htmlattrxref("minlength", "input")}}, {{htmlattrxref("multiple", "input")}}, {{htmlattrxref("pattern", "input")}}, {{htmlattrxref("placeholder", "input")}}, {{htmlattrxref("readonly", "input")}}, and {{htmlattrxref("size", "input")}}</td>
</tr>
<tr>
<td><strong>IDL 属性</strong></td>
<td><code>list</code> and <code>value</code></td>
</tr>
<tr>
<td><strong>方法</strong></td>
<td>{{domxref("HTMLInputElement.select", "select()")}}</td>
</tr>
</tbody>
</table>
<h2 id="值">值</h2>
<p>{{HTMLElement("input")}} 元素的 {{htmlattrxref("value", "input")}} 属性包含一个会自动验证是否符合电子邮件语法的 {{domxref("DOMString")}}。具体来说,此处有三个格式可以通过验证:</p>
<ol>
<li>一个空字符串(“”),表示用户未输入值或该值已被删除。</li>
<li>
<p>单个符合格式的电子邮件地址。并不代表该邮件地址存在,而是说它至少在格式上是正确的,简单地讲就是 <code>"username@domain"</code> 或者 <code>"username@domain.tld"</code>,当然并不局限于此。关于匹配电子邮件地址验证算法的 {{Glossary("regular expression")}},参见 <a href="#validation">Validation</a>。</p>
</li>
<li>
<p>当且仅当明确 {{htmlattrxref("multiple", "input")}} 属性时,值可以是一个列表,该列表包含一串符合格式的电子邮件地址,且每个地址之间用逗号分隔。列表中,位于每个地址之前和之后的任何空白字符都会被移除。</p>
</li>
</ol>
<p>关于如何验证电子邮件地址以保证其格式正确的细节,参见 <a href="#validation">Validation</a>。</p>
<h2 id="Using_email_inputs">Using email inputs</h2>
<p>电子邮件地址是网络上最频繁输入的文本数据表格; 登录网站,请求信息,允许订单确认,网络邮件等时使用它们。 因此,“电子邮件” 输入类型可以使您作为 Web 开发人员的工作变得更加容易,因为它可以在构建电子邮件地址的用户界面和逻辑时帮助简化您的工作。 当您使用正确的类型值 “email” 创建电子邮件输入时,您将自动验证输入的文本是否至少以正确的形式可能是合法的电子邮件地址。 这有助于避免用户输错地址或提供无效地址的情况。</p>
<p>然而,重要的是要注意,这不足以确保指定的文本是实际存在的电子邮件地址,对应于站点的用户,或者以任何其他方式可接受。 它只是确保将字段的值正确格式化为电子邮件地址。</p>
<div class="note">
<p><strong>注意:记住用户可以在幕后修改 HTML 也很重要,因此您的站点不得出于任何安全目的使用此验证。 您必须验证所提供文本可能具有任何安全隐患的任何事务的服务器端的电子邮件地址。</strong></p>
</div>
<h3 id="A_simple_email_input">A simple email input</h3>
<p>目前,所有实现此元素的浏览器都将其实现为具有基本验证功能的标准文本输入字段。 但是,该规范允许浏览器对此进行纬度调整。 例如,该元素可以与用户设备的内置地址簿集成,以允许从该列表中挑选电子邮件地址。 在最基本的形式中,电子邮件输入可以像这样实现:</p>
<pre class="brush: html"><input id="emailAddress" type="email"></pre>
<p>{{ EmbedLiveSample('A_simple_email_input', 600, 40) }}</p>
<p>Notice that it's considered valid when empty and when a single validly-formatted email address is entered, but is otherwise not considered valid. By adding the {{htmlattrxref("required", "input")}} attribute, only validly-formed email addresses are allowed; the input is no longer considered valid when empty.</p>
<h3 id="Allowing_multiple_email_addresses">Allowing multiple email addresses</h3>
<p>By adding the {{htmlattrxref("multiple", "input")}} Boolean attribute, the input can be configured to accept multiple email addresses.</p>
<pre class="brush: html"><input id="emailAddress" type="email" multiple></pre>
<p>{{ EmbedLiveSample('Allowing_multiple_email_addresses', 600, 40) }}</p>
<p>The input is now considered valid when a single email address is entered, or when any number of email addresses separated by commas and, optionally, some number of whitespace characters are present.</p>
<div class="note">
<p><strong>Note</strong>: When <code>"multiple"</code> is used, the value <em>is</em> allowed to be empty.</p>
</div>
<p>Some examples of valid strings when <code>"multiple"</code> is specified:</p>
<ul>
<li><code>""</code></li>
<li><code>"me@example"</code></li>
<li><code>"me@example.org"</code></li>
<li><code>"me@example.org,you@example.org"</code></li>
<li><code>"me@example.org, you@example.org"</code></li>
<li><code>"me@example.org,you@example.org, us@example.org"</code></li>
</ul>
<p>Some examples of invalid strings:</p>
<ul>
<li><code>","</code></li>
<li><code>"me"</code></li>
<li><code>"me@example.org you@example.org"</code></li>
</ul>
<h3 id="Placeholders">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>"email"</code> input with the placeholder <code>"sophie@example.com"</code>. Note how the placeholder disappears and reappears as you manipulate the contents of the edit field.</p>
<pre class="brush: html"><input type="email" placeholder="sophie@example.com"></pre>
<p>{{ EmbedLiveSample('Placeholders', 600, 40) }}</p>
<h3 id="Controlling_the_input_size">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">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 the email edit box is 15 characters wide:</p>
<pre class="brush: html"><input type="email" size="15"></pre>
<p>{{ EmbedLiveSample('Physical_input_element_size', 600, 40) }}</p>
<h4 id="Element_value_length">Element value length</h4>
<p>The <code>size</code> is separate from the length limitation on the entered email address itself so that you can have fields fit in a small space while still allowing longer email address strings to be entered. You can specify a minimum length, in characters, for the entered email address using the {{htmlattrxref("minlength", "input")}} attribute; similarly, use {{htmlattrxref("maxlength", "input")}} to set the maximum length of the entered email address.</p>
<p>The example below creates a 32 character-wide email address entry box, requiring that the contents be no shorter than 3 characters and no longer than 64 characters.</p>
<pre class="brush: html"><input type="email" size="32" minlength="3" maxlength="64"></pre>
<p>{{EmbedLiveSample("Element_value_length", 600, 40) }}</p>
<h3 id="Providing_default_options">Providing default options</h3>
<p>As always, you can provide a default value for an <code>"email"</code> input box by setting its {{htmlattrxref("value", "input")}} attribute:</p>
<div id="Default_value">
<pre class="brush: html"><input type="email" value="default@example.com"></pre>
</div>
<p>{{EmbedLiveSample("Default_value", 600, 40)}}</p>
<h4 id="Offering_suggested_values">Offering suggested values</h4>
<p>Taking it a step farther, you can provide a list of default options from which the user can select by specifying the {{htmlattrxref("list", "input")}} attribute. This doesn't limit the user to those options, but does allow them to select commonly-used email addresses more quickly. This also offers hints to {{htmlattrxref("autocomplete", "input")}}. The <code>list</code> attribute specifies the ID of a {{HTMLElement("datalist")}}, 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 email entry box.</p>
<pre class="brush: html"><input type="email" size="40" list="defaultEmails">
<datalist id="defaultEmails">
<option value="jbond007@mi6.defence.gov.uk">
<option value="jbourne@unknown.net">
<option value="nfury@shield.org">
<option value="tony@starkindustries.com">
<option value="hulk@grrrrrrrr.arg">
</datalist></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 filtered to show only matching values. Each typed character narrows down the list until the user makes a selection or types a custom value.</p>
<p><img alt="Animation: Using keyboard entry to filter the list of suggested email addresses" src="https://mdn.mozillademos.org/files/14831/html-input-email1.gif" style="border-style: solid; border-width: 1px; height: 168px; width: 352px;"></p>
<h2 id="Validation">Validation</h2>
<p>There are two levels of content validation available for <code>"email"</code> inputs. First, there's the standard level of validation offered to all {{HTMLElement("input")}}s, which automatically ensures that the contents meet the requirements to be a valid email address. But there's also the option to add additional filtering to ensure that your own specialized needs are met, if you have any.</p>
<div class="warning">
<p><strong>重要提示:HTML 表单验证不能替代确保输入的数据格式正确的脚本。 对于某些人来说,调整 HTML 非常容易,因为它允许他们绕过验证,或者完全删除它。 某人也可以完全绕过您的 HTML 并将数据直接提交给您的服务器。 如果您的服务器端代码无法验证它收到的数据,则当数据格式不正确(或数据太大,类型错误等等)输入数据库时,灾难可能会发生。</strong></p>
</div>
<h3 id="Basic_validation">Basic validation</h3>
<p>支持 “电子邮件” 输入类型的浏览器会自动提供验证,以确保只有符合 Internet 电子邮件地址标准格式的文本才会输入到输入框中。 实现规范的浏览器应使用与以下正则表达式等效的算法:</p>
<pre class="brush: js">/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}
[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
</pre>
<p>To learn more about how form validation works and how to take advantage of the {{cssxref(":valid")}} and {{cssxref(":invalid")}} CSS properties to style the input based on whether or not the current value is valid, see <a href="/en-US/docs/Learn/HTML/Forms/Form_validation">Form data validation</a>.</p>
<div class="note">
<p><strong>Note</strong>: There are known specification issues related to international domain names and the validation of email addresses in HTML. See <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=15489">W3C bug 15489</a> for details.</p>
</div>
<h3 id="Pattern_validation">Pattern validation</h3>
<p>If you need the entered email address to be restricted further than just "any string that looks like an email address," you can use the {{htmlattrxref("pattern", "input")}} attribute to specify a <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">regular expression</a> the value must match for it to be valid. If the {{htmlattrxref("multiple", "input")}} attribute is specified, each individual item in the comma-delineated list of values must match the {{jsxref("regexp")}}.</p>
<p>For example, let's say you're building a page for employees of Best Startup Ever, Inc. which will let them contact their IT department for help. In our simplified form, the user needs to enter their email address and a message describing the problem they need help with. We want to ensure that not only does the user provide a valid email address, but for security purposes, we require that the address be an internal corporate email address.</p>
<p>Since inputs of type <code>"email"</code> validate against both the standard email address validation <em>and</em> the specified {{htmlattrxref("pattern", "input")}}, you can implement this easily. Let's see how:</p>
<div class="hidden">
<pre class="brush: css">body {
font: 16px sans-serif;
}
.emailBox {
padding-bottom: 20px;
}
.messageBox {
padding-bottom: 20px;
}
label {
line-height: 22px;
}
label::after {
content: ":";
}</pre>
</div>
<pre class="brush: html"><form>
<div class="emailBox">
<label for="emailAddress">Your email address</label><br>
<input id="emailAddress" type="email" size="64" maxLength="64" required
placeholder="username@beststartupever.com" pattern=".+@beststartupever.com"
title="Please provide only a Best Startup Ever corporate email address">
</div>
<div class="messageBox">
<label for="message">Request</label><br>
<textarea id="message" cols="80" rows="8" required
placeholder="My shoes are too tight, and I have forgotten how to dance."></textarea>
</div>
<input type="submit" value="Send Request">
</form>
</pre>
<p>{{EmbedLiveSample("Pattern_validation", 700, 275)}}</p>
<p>Our {{HTMLElement("form")}} contains one {{HTMLElement("input")}} of type <code>"email"</code> for the user's email address, a {{HTMLElement("textarea")}} to enter their message for IT into, and an <code><input></code> of type <code><a href="/en-US/docs/Web/HTML/Element/input/submit">"submit"</a></code>, which creates a button to submit the form. Each text entry box has a {{HTMLElement("label")}} associated with it to let the user know what's expected of them.</p>
<p>Let's take a closer look at the email address entry box. Its {{htmlattrxref("size", "input")}} and {{htmlattrxref("maxlength", "input")}} attributes are both set to 64 in order to show room for 64 characters worth of email address, and to limit the number of characters actually entered to a maximum of 64. The {{htmlattrxref("required", "input")}} attribute is specified, making it mandatory that a valid email address be provided.</p>
<p>An appropriate {{htmlattrxref("placeholder", "input")}} is provided—<code>"username@beststartupever.com"</code>—to demonstrate what constitutes a valid entry. This string demonstrates both that an email address should be entered, and suggests that it should be a corporate beststartupever.com account. This is in addition to the fact that using type <code>"email"</code> will validate the text to ensure that it's formatted like an email address. If the text in the input box isn't an email address, you'll get an error message that looks something like this:</p>
<p><img alt="" src="https://mdn.mozillademos.org/files/14855/enter-valid-email-address.png" style="height: 125px; width: 530px;"></p>
<p>If we left things at that, we would at least be validating on legitimate email addresses. But we want to go one step farther: we want to make sure that the email address is in fact in the form "<em>username</em>@beststartupever.com". This is where we'll use {{htmlattrxref("pattern", "input")}}. We set <code>pattern</code> to <code>".+@beststartupever.com"</code>. This simple regular expression requests a string that consists of at least one character of any kind, then an "@" followed by the domain name "beststartupever.com".</p>
<p>Note that this is not even close to an adequate filter for valid email addresses; it would allow things such as " @beststartupever.com" (note the leading space) or "@@beststartupever.com", neither of which is valid. However, the browser runs both the standard email address filter <em>and</em> our custom pattern against the specified text. As a result, we wind up with a validation which says "make sure this is a valid email address, and if it is, make sure it's also a beststartupever.com address."</p>
<p>It's advisable to use the {{htmlattrxref("title")}} attribute along with <code>pattern</code>. If you do, the <code>title</code> <em>must</em> describe the pattern. That is, it should explain what format the data should take on, rather than any other information. That's because the <code>title</code> may be displayed or spoken as part of a validation error message. For example, the browser might present the message "The entered text doesn't match the required pattern." followed by your specified <code>title</code>. If your <code>title</code> is something like "Email address", the result would be the message "The entered text doesn't match the required pattern. Email address", which isn't very good.</p>
<p>That's why, instead, we specify the string "Please provide only a Best Startup Ever corporate email address" By doing that, the resulting full error message might be something like "The entered text doesn't match the required pattern. Please provide only a Best Startup Ever corporate email address."</p>
<p><img alt="" src="https://mdn.mozillademos.org/files/14853/email-pattern-match-bad.png" style="height: 140px; width: 536px;"></p>
<div class="note">
<p><strong>Note</strong>: If you run into trouble while writing your validation regular expressions and they're not working properly, check your browser's console; there may be helpful error messages there to aid you in solving the problem.</p>
</div>
<h2 id="Examples">Examples</h2>
<p>Here we have an email input with the ID <code>"emailAddress"</code> which is allowed to be up to a maximum of 256 characters long. The input box itself is physically 64 characters wide, and displays the text <code>"user@example.gov"</code> as a placeholder anytime the field is empty. In addition, by using the {{htmlattrxref("multiple", "input")}} attribute, the box is configured to allow the user to enter zero or more email addresses, separated by commas, as described in <a href="#allowing_multiple_email_addresses">Allowing multiple email addresses</a>. As a final touch, the {{htmlattrxref("list", "input")}} attribute contains the ID of a {{HTMLElement("datalist")}} whose {{HTMLElement("option")}}s specify a set of suggested values the user can choose from.</p>
<p>As an added touch, the {{HTMLElement("label")}} element is used to establish a label for the email entry box, with its {{htmlattrxref("for", "label")}} attribute referencing the <code>"emailAddress"</code> ID of the {{HTMLElement("input")}} element. By associating the two elements in this way, clicking on the label will focus the input element.</p>
<pre class="brush: html"><label for="emailAddress">Email</label><br/>
<input id="emailAddress" type="email" placeholder="user@example.gov"
list="defaultEmails" size="64" maxlength="256" multiple>
<datalist id="defaultEmails">
<option value="jbond007@mi6.defence.gov.uk">
<option value="jbourne@unknown.net">
<option value="nfury@shield.org">
<option value="tony@starkindustries.com">
<option value="hulk@grrrrrrrr.arg">
</datalist></pre>
<p>{{EmbedLiveSample('Examples', 600, 50)}}</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', 'forms.html#e-mail-state-(type=email)', '<input type="email">')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td>Initial definition</td>
</tr>
<tr>
<td>{{SpecName('HTML5.1', 'sec-forms.html#email-state-typeemail', '<input type="email">')}}</td>
<td>{{Spec2('HTML5.1')}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("html.elements.input.input-email")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Learn/HTML/Forms">HTML forms guide</a></li>
<li>{{HTMLElement("input")}}</li>
<li><code><a href="/en-US/docs/Web/HTML/Element/input/tel"><input type="tel"></a></code></li>
</ul>
|