Help us understand the problem. What is going on with this article?

input type="email" の値の書式

フォームの入力欄で type="email" を使うと、入力値がメールアドレスとして正しいかどうかをブラウザーに判別してもらうことができます。

<input type="email" name="email">

そのメールアドレスの書式は具体的にどんなものかというと、HTML5の仕様書に正規表現で提示されています。

https://html.spec.whatwg.org/#e-mail-state-(type=email)

The following JavaScript- and Perl-compatible regular expression is an implementation of the above definition.

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

この正規表現には1つ問題がありまして、それは taro@hogehoge のように . を含まないドメイン名のアドレスはアリであることです。hogehoge はドメイン名の書式としてアリなので、そりゃそうです。

といっても、普通は taro@hogehoge は禁止したいですよね。それには、次の正規表現を使います。最後の *+ に変えるだけ。

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

これを inputpattern属性に指定すると、taro@hogehoge が送信できないようにできます。

<input type="email" name="email"
  pattern="^[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])?)+$" >
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away