<input type="email">

この翻訳は不完全です。英語から この記事を翻訳 してください。

"email" 型の <input> 要素は、ユーザーに一つのメールアドレス、または、 multiple 属性が設定されていた場合は、メールアドレスのリストを入力又は編集させるために使用します。入力値はフォームの送信前に自動的に検証され、空欄又は正しい形式のメールアドレス(またはメールアドレスのリスト)であることが確認されます。状態に応じて CSS の :valid 及び :invalid 疑似クラスが自動的に適用されます。

メモ: "email" 型に対応していないブラウザーでは、通常の "text" 入力欄で代替されます。

<input id="emailAddress" type="email">

メールアドレスを表す DOMString、又は空欄
イベント change 及び input
対応する共通属性 autocomplete, list, maxlength, minlength, multiple, pattern, placeholder, readonly, size
IDL 属性 list, value
メソッド select()

<input> 要素の value 属性にはメールアドレスの構文に合うかどうかが自動的に検証される DOMString が入ります。具体的には、検証に通ることができる値の書式には3種類があります。

  1. 空文字列 ("")。ユーザーが値を入力しないか、又は値が削除されたことを表します。
  2. 単一の正しい形式のメールアドレス。メールアドレスが実在する必要はありませんが、少なくとも正しい書式です。簡単に言えば、 "username@domain.tld" の形です。もちろんそれ以上のものもあります。メールアドレスの検証アルゴリズムで一致する regexp については、検証の節を参照してください。
  3. multiple 属性が指定されたとき(のみ)、値はコンマで区切って複数の正しい形式のメールアドレスを入れることができます。それぞれのメールアドレスの前後にある空白は除去されます。

どのようにメールアドレスが正しい形式であると検証されるかの詳細は、検証の節を参照してください。

email 入力欄の使用

メールアドレスはウェブで最も頻繁に入力される文字列データです。ウェブサイトにログインするときに使われたり、情報をリクエストしたり、注文の確認をできるようにしたり、ウェブメールでつかったりなどです。そのような場合、 "email" 入力型はユーザーインターフェイスやメールアドレスの処理の構築の作業を簡略化することができるので、ウェブ開発者の仕事をはるかに楽にすることができます。メールアドレスの入力欄を、 type の値を正しく "email" にして作成すれば、入力された文字列が少なくとも合法のメールアドレスである可能性がある但し書式であることを自動的に検証してくれます。これにより、ユーザーがアドレスを打ち間違えたり、無効なアドレスを入力したりすることを防ぐのに役立ちます。

しかし、指定された文字列が、実際に存在するメールアドレスであるかどうか、サイトのユーザーに一致するか、他の方法で利用できるかを保証するには不十分です。単に入力欄の値がメールアドレスの形式に沿っているかを保証するだけです。

メモ: ユーザーが HTML をその場面の裏でいじることができることを意識しておくことは極めて重要です。ですから、安全を目的として、サイトでクライアント側の値検証機能のみを使用してはいけません。何らかのセキュリティ上の問題を含む可能性がある値が提供されるトランザクションの場合は、いずれもサーバー側で値検証を行う必要があります

単純な email 型の入力欄

現在、この要素を実装しているすべてのブラウザーが、これを基本的な検証機能がついた標準のテキスト入力欄として実装しています。しかし、仕様書ではブラウザーに自由度を許容しています。例えば、この要素はユーザーの機器に内蔵されたアドレス帳と統合され、メールアドレスをリストから選ぶことができるようにすることも可能です。多くの基本的なフォームでは、 email 入力欄は次のように実装されています。

<input id="emailAddress" type="email">

なお、妥当と判断されるのは空欄の場合と、単一の妥当な書式のメールアドレスが入力されている場合で、それ以外は妥当であるとは判断されません。 required 属性を追加することで、妥当な書式のメールアドレスのみが許容されるようになり、空欄の場合は妥当であるとは判断されなくなります。

複数のメールアドレスの入力

By adding the multiple Boolean attribute, the input can be configured to accept multiple email addresses.

<input id="emailAddress" type="email" multiple>

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.

Note: When "multiple" is used, the value is allowed to be empty.

Some examples of valid strings when "multiple" is specified:

  • ""
  • "me@example"
  • "me@example.org"
  • "me@example.org,you@example.org"
  • "me@example.org, you@example.org"
  • "me@example.org,you@example.org, us@example.org"

Some examples of invalid strings:

  • ","
  • "me"
  • "me@example.org you@example.org"

プレイスホルダー

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 <input>. This is where placeholders come in. A placeholder is a value that demonstrates the form the value should take by presenting an example of a valid value, which is displayed inside the edit box when the element's value is "". Once data is entered into the box, the placeholder disappears; if the box is emptied, the placeholder reappears.

Here, we have an "email" input with the placeholder "sophie@example.com". Note how the placeholder disappears and reappears as you manipulate the contents of the edit field.

<input type="email" placeholder="sophie@example.com">

入力欄の寸法の制御

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.

物理的な入力欄の寸法

The physical size of the input box can be controlled using the size 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:

<input type="email" size="15">

要素の値の長さ

The size 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 minlength attribute; similarly, use maxlength to set the maximum length of the entered email address.

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.

<input type="email" size="32" minlength="3" maxlength="64">

既定のオプションの提供

As always, you can provide a default value for an "email" input box by setting its value attribute:

<input type="email" value="default@example.com"

Offering suggested values

Taking it a step farther, you can provide a list of default options from which the user can select by specifying the list 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 autocomplete. The list attribute specifies the ID of a <datalist>, which in turn contains one <option> element per suggested value; each option's value is the corresponding suggested value for the email entry box.

<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>

With the <datalist> element and its <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.

Animation: Using keyboard entry to filter the list of suggested email addresses

検証

There are two levels of content validation available for "email" inputs. First, there's the standard level of validation offered to all <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.

Important: HTML form validation is not 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 completely. 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.

基本的な検証

Browsers that support the "email" input type automatically provide validation to ensure that only text that matches the standard format for Internet email addresses is entered into the input box. Browsers that implement the specification should be using an algorithm equivalent to the following regular expression:

/^[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])?)*$/

To learn more about how form validation works and how to take advantage of the :valid and :invalid CSS properties to style the input based on whether or not the current value is valid, see Form data validation.

Note: There are known specification issues related to international domain names and the validation of email addresses in HTML. See W3C bug 15489 for details.

パターンの検証

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 pattern attribute to specify a regular expression the value must match for it to be valid. If the multiple attribute is specified, each individual item in the comma-delineated list of values must match the regexp.

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.

Since inputs of type "email" validate against both the standard email address validation and the specified pattern, you can implement this easily. Let's see how:

<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>

Our <form> contains one <input> of type "email" for the user's email address, a <textarea> to enter their message for IT into, and an <input> of type "submit", which creates a button to submit the form. Each text entry box has a <label> associated with it to let the user know what's expected of them.

Let's take a closer look at the email address entry box. Its size and maxlength 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 required attribute is specified, making it mandatory that a valid email address be provided.

An appropriate placeholder is provided—"username@beststartupever.com"—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 "email" 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:

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 "username@beststartupever.com". This is where we'll use pattern.  We set pattern to ".+@beststartupever.com". 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".

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 and 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."

It's advisable to use the title attribute along with pattern. If you do, the title must describe the pattern. That is, it should explain what format the data should take on, rather than any other information. That's because the title 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 title. If your title 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.

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."

Note: 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.

Here we have an email input with the ID "emailAddress" 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 "user@example.gov" as a placeholder anytime the field is empty. In addition, by using the multiple attribute, the box is configured to allow the user to enter zero or more email addresses, separated by commas, as described in Allowing multiple email addresses. As a final touch, the list attribute contains the ID of a <datalist> whose <option>s specify a set of suggested values the user can choose from.

As an added touch, the <label> element is used to establish a label for the email entry box, with its for attribute referencing the "emailAddress" ID of the <input> element. By associating the two elements in this way, clicking on the label will focus the input element.

<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>

仕様書

仕様書 策定状況 コメント
HTML Living Standard
<input type="email"> の定義
現行の標準 初回定義
HTML 5.1
<input type="email"> の定義
勧告 初回定義

ブラウザーの対応

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

機能 Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
基本対応 5.0 ? Unknown (4.0) 10 10.62 ?
機能 Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile iOS WebKit
(Safari/Chrome/Firefox/etc)
基本対応 ? ? ? 4.0 (4.0) ? (有) 3.1[1]

[1] Safari doesn't do validation, but instead offers a custom "email" keyboard, which is designed to make entering email addresses easier. Also, note that Safari mobile on iOS devices automatically applies a default style of opacity: 0.4 to disable textual <input> elements, including those of type "email". Other major browsers don't currently share this particular default style.

関連情報

ドキュメントのタグと貢献者

このページの貢献者: mfuji09
最終更新者: mfuji09,