新しいツールを追加しました! 新着を見る

ホーム/コンポーネント/お問い合わせフォーム

お問い合わせフォーム examples

ネイティブバリデーション、インラインエラーメッセージ、アクセシブルな送信状態を備えた、ラベル付きのお問い合わせフォーム。

表示されたラベル、インラインのバリデーションメッセージ、アクセシブルな送信状態を備えたお問い合わせフォーム。すべてのフィールドがラベルと正しく関連付けられており、エラーはaria-describedbyを介して配線されているため、表示されるだけでなくアナウンスされます。

最初のバリアントは送信時にバリデーションを行い、フィールドごとのエラーを描画します。その他のバリアントは、ブラウザのネイティブバリデーションに依存する静的なマークアップです。

Contact form with inline validation

Name, email, and message with labels connected by id, per-field error messages wired through aria-describedby, and a submit button that guards against double sends.

Preview
"use client";

import { useState } from "react";

type Errors = Partial<Record<"name" | "email" | "message", string>>;

export default function ContactForm() {
  const [errors, setErrors] = useState<Errors>({});
  const [status, setStatus] = useState<"idle" | "sending" | "sent">("idle");

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();

    const data = new FormData(event.currentTarget);
    const next: Errors = {};

    if (!String(data.get("name") ?? "").trim()) {
      next.name = "Enter your name.";
    }
    if (!String(data.get("email") ?? "").includes("@")) {
      next.email = "Enter a valid email address.";
    }
    if (!String(data.get("message") ?? "").trim()) {
      next.message = "Enter a message.";
    }

    setErrors(next);
    if (Object.keys(next).length > 0) return;

    setStatus("sending");
    // await fetch("/api/contact", { method: "POST", body: data });
    setStatus("sent");
  }

  const field =
    "mt-1.5 block w-full rounded-md border px-3 py-2 text-sm text-gray-900 focus:outline-none focus:ring-1";
  const ok = "border-gray-300 focus:border-gray-900 focus:ring-gray-900";
  const bad = "border-red-500 focus:border-red-500 focus:ring-red-500";

  return (
    <form onSubmit={handleSubmit} className="mx-auto max-w-lg space-y-5" noValidate>
      <div>
        <label htmlFor="name" className="block text-sm font-medium text-gray-900">
          Name
        </label>
        <input
          id="name"
          name="name"
          type="text"
          autoComplete="name"
          aria-invalid={Boolean(errors.name)}
          aria-describedby={errors.name ? "name-error" : undefined}
          className={field + " " + (errors.name ? bad : ok)}
        />
        {errors.name && (
          <p id="name-error" className="mt-1.5 text-xs text-red-600">
            {errors.name}
          </p>
        )}
      </div>

      <div>
        <label htmlFor="email" className="block text-sm font-medium text-gray-900">
          Email
        </label>
        <input
          id="email"
          name="email"
          type="email"
          autoComplete="email"
          aria-invalid={Boolean(errors.email)}
          aria-describedby={errors.email ? "email-error" : "email-hint"}
          className={field + " " + (errors.email ? bad : ok)}
        />
        {errors.email ? (
          <p id="email-error" className="mt-1.5 text-xs text-red-600">
            {errors.email}
          </p>
        ) : (
          <p id="email-hint" className="mt-1.5 text-xs text-gray-500">
            We will only use this to reply.
          </p>
        )}
      </div>

      <div>
        <label htmlFor="message" className="block text-sm font-medium text-gray-900">
          Message
        </label>
        <textarea
          id="message"
          name="message"
          rows={5}
          aria-invalid={Boolean(errors.message)}
          aria-describedby={errors.message ? "message-error" : undefined}
          className={field + " " + (errors.message ? bad : ok)}
        />
        {errors.message && (
          <p id="message-error" className="mt-1.5 text-xs text-red-600">
            {errors.message}
          </p>
        )}
      </div>

      <button
        type="submit"
        disabled={status === "sending"}
        className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 disabled:opacity-60 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2"
      >
        {status === "sending" ? "Sending..." : "Send message"}
      </button>

      <p aria-live="polite" className="text-sm text-gray-600">
        {status === "sent" ? "Thanks - we will reply shortly." : ""}
      </p>
    </form>
  );
}

Contact form with two-column fields

Name and email side by side from the sm breakpoint, stacked below it. Only pair fields that are genuinely related.

Preview
export default function TwoColumnContactForm() {
  return (
    <form className="mx-auto max-w-lg space-y-5" noValidate>
      <div className="grid gap-5 sm:grid-cols-2">
        <div>
          <label htmlFor="name" className="block text-sm font-medium text-gray-900">Name</label>
          <input id="name" name="name" type="text" required autoComplete="name" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
        <div>
          <label htmlFor="email" className="block text-sm font-medium text-gray-900">Email</label>
          <input id="email" name="email" type="email" required autoComplete="email" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
      </div>

      <div>
        <label htmlFor="message" className="block text-sm font-medium text-gray-900">Message</label>
        <textarea id="message" name="message" rows="5" required className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"></textarea>
      </div>

      <button type="submit" className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Send message</button>
    </form>
  );
}

Contact form with a subject select

A subject dropdown for routing enquiries to the right inbox. Keep the options short and concrete rather than naming internal teams.

Preview
export default function ContactFormWithSubject() {
  return (
    <form className="mx-auto max-w-lg space-y-5" noValidate>
      <div>
        <label htmlFor="name" className="block text-sm font-medium text-gray-900">Name</label>
        <input id="name" name="name" type="text" required autoComplete="name" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
      </div>
      <div>
        <label htmlFor="email" className="block text-sm font-medium text-gray-900">Email</label>
        <input id="email" name="email" type="email" required autoComplete="email" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
      </div>

      <div>
        <label htmlFor="subject" className="block text-sm font-medium text-gray-900">Subject</label>
        <select id="subject" name="subject" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900 bg-white">
          <option>General question</option>
          <option>Sales and pricing</option>
          <option>Technical support</option>
          <option>Press</option>
        </select>
      </div>

      <div>
        <label htmlFor="message" className="block text-sm font-medium text-gray-900">Message</label>
        <textarea id="message" name="message" rows="5" required className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"></textarea>
      </div>

      <button type="submit" className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Send message</button>
    </form>
  );
}

Inline newsletter signup form

A single email field with the button on the same row, for footers and banners where a full form is too heavy.

Preview
export default function NewsletterForm() {
  return (
    <form className="mx-auto max-w-md" noValidate>
      <label htmlFor="newsletter-email" className="block text-sm font-medium text-gray-900">Get product updates</label>
      <div className="mt-1.5 flex gap-2">
        <input
          id="newsletter-email"
          name="email"
          type="email"
          required
          autoComplete="email"
          placeholder="you@example.com"
          className="block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"
        />
        <button type="submit" className="shrink-0 rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Subscribe</button>
      </div>
      <p className="mt-2 text-xs text-gray-500">One email a month. Unsubscribe any time.</p>
    </form>
  );
}

Contact section with details beside the form

Contact details on the left and the form on the right at md, stacked on mobile. A complete contact-page section.

Preview
export default function ContactSection() {
  return (
    <section className="mx-auto grid max-w-5xl gap-10 px-4 py-12 md:grid-cols-2">
      <div className="bg-gray-900 p-4 rounded-lg">
        <h2 className="text-2xl font-bold text-gray-100">Get in touch</h2>
        <p className="mt-3 text-sm text-white">Tell us what you are working on and we will reply within one business day.</p>

        <dl className="mt-8 space-y-4 text-sm">
          <div>
            <dt className="font-medium text-gray-100">Email</dt>
            <dd className="text-white">hello@example.com</dd>
          </div>
          <div>
            <dt className="font-medium text-gray-100">Office hours</dt>
            <dd className="text-white">Monday to Friday, 9am to 5pm</dd>
          </div>
        </dl>
      </div>

      <form className="space-y-5" noValidate>
        <div>
          <label htmlFor="name" className="block text-sm font-medium text-gray-900">Name</label>
          <input id="name" name="name" type="text" required autoComplete="name" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
        <div>
          <label htmlFor="email" className="block text-sm font-medium text-gray-900">Email</label>
          <input id="email" name="email" type="email" required autoComplete="email" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
        </div>
        <div>
          <label htmlFor="message" className="block text-sm font-medium text-gray-900">Message</label>
          <textarea id="message" name="message" rows="5" required className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900"></textarea>
        </div>

        <button type="submit" className="w-full rounded-md bg-gray-900 px-4 py-2.5 text-sm font-medium text-white hover:bg-gray-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2">Send message</button>
      </form>
    </section>
  );
}

What is a お問い合わせフォーム?

お問い合わせフォームは、訪問者がサイトの運営者に連絡をとるための最も一般的な方法であり、実装のわずかなミスが直接メッセージの損失につなが数少ない場所の一つです。必須フィールドが1つ増えるごとに、不明瞭なエラーが1つあるごとに、そしてオートフィルが認識できないインプットが1つあるごとに、せっかく連絡しようと決意したその瞬間に摩擦が生じます。

基本事項は地味であり、しばしばスキップされがちです。すべてのインプレットには、プレースホルダーの代用ではなく、idで接続された実際のlabel要素が必要です。プレースホルダーは入力開始時に消えてしまうため、どこに入力していたかわからなくなった人がフィールドの意味を思い出す方法がなくなり、スクリーンリーダーでも確実にアナウンスされません。autocomplete属性も同じ実用的な理由から重要です。これがあればブラウザが名前とメールアドレスを1タップで入力してくれますが、これがないとスマホのキーボードで両方とも手動で入力することになります。

バリデーションは、ほとんどのフォームがフラストレーションを生む原因となる箇所です。キーストロークごとにバリデーションを行うと、まだ3文字目を入力している最中に「メールアドレスが無効です」と告げることになります。送信時のみバリデーションを行い、説明もなくフィールドを赤くハイライトするだけでは、ユーザーは推測するしかなくなります。うまく機能する中間地点は、フィールドごとに具体的なメッセージを表示して送信時にバリデーションを行い、修正されるとそのエラーが消えるように修正しながらフィールドを再バリデーションすることです。

送信状態は、見た目以上に重要です。フィードバックを返さないボタンは2回目のクリックを誘発し、保護されていないフォームでの2回目のクリックは重複メッセージを送信します。送信中にボタンを無効化してラベルを変更することでこれに対処し、politeなライブリージョンによって、メッセージの出現を目で確認できない人にも最終的な確認が伝えられます。

Anatomy

フィールドグループ
ラベル、コントロール、ヒントまたはエラーメッセージをまとめたもので、フィールド間で3つの要素間の余白が一定に保たれるようにラップされています。
Label
インプットのidと一致するfor属性を持つ、実際のlabel要素。これにより、ラベルがクリック可能になり、インプットにアクセシブルな名前が付与されます。
Control
フォームデータ用のname、正しいモバイルキーボード用のtype、ブラウザ入力用のautocompleteトークンを持つ、インプットまたはテキストエリア。
ヒント
aria-describedbyで接続されたオプションのサポートテキスト。通常プレースホルダーに入れるような内容に使用されます。
エラーメッセージ
フィールドが無効な場合にヒントを置き換えます。aria-describedbyとも接続され、コントロールのaria-invalidとペアになります。
Submit button
Enterキーでフォームを送信できるようにするための実際の送信ボタン。送信中は無効化され、現在の状態を反映したラベルが表示されます。
ステータスリージョン
成功メッセージを伝えるpoliteなライブリージョン。画面上に表示されるだけでなく、確認が音声等でアナウンスされます。

How to Use

  1. 1

    フォームをコピー

    ブラウザのネイティブバリデーションに依存する場合はHTML版を、カスタムメッセージや制御された送信状態が必要な場合はReact版をご利用ください。

  2. 2

    エンドポイントを接続

    コメントアウトされたfetch呼び出しを、APIルート、フォームサービス、またはサーバーアクションに置き換えてください。フォームデータはすでにFormDataオブジェクトとして収集されています。

  3. 3

    フィールドを調整

    必要に応じてフィールドグループを追加または削除します。それぞれに対応するlabelのforとinputのidのペアが必要です。その接続によってフィールドが使用可能になります。

  4. 4

    autocompleteトークンを設定

    標準的な値(name、email、tel、organization)を使用します。これにより、ブラウザがスマホでワンタップでフォームに入力できるようになります。

  5. 5

    レスポンスの処理

    成功時はステータスを送信済みに設定し、失敗時は具体的なメッセージを表示します。成功したように見えて実は失敗している状態は、エラーそのものよりも悪質です。

Features

すべてのフィールドに実際のラベルを配置する

各ラベルはidで紐づけられており、クリック可能で入力欄にアクセシブルな名前を提供します。プレースホルダーが不完全にその役割を代行するわけではありません。

フィールドに紐づけられたエラー

メッセージはaria-describedbyで接続され、コントロールにはaria-invalidが付与されるため、エラーは目で見えるだけでなく、フィールドにフォーマットが当たった際にも読み上げられます。

Autocompleteのサポート

すべてのフィールドに標準トークンを設定することで、ブラウザやパスワードマネージャーが1回のアクションでフォームに入力できるようになり、ワンタップとスマホでの長文入力を分ける境界線となります。

二重送信防止付き送信

送信中はボタンが無効化されラベルが変更されるため、重複したメッセージを送信するダブルクリックを防ぎます。

読み上げられる確認メッセージ

成功メッセージはpoliteなlive region内に配置されるため、静かに表示されるだけでなく、スクリーンリーダーユーザーにも確実に届きます。

適切なモバイルキーボード

フィールドごとに適切なinputタイプが設定されているため、メールアドレス入力欄では「@」マークが含まれるキーボードが立ち上がり、ユーザーが記号を探す手間を省きます。

Common Uses

サイトのお問い合わせページ

一般的な問い合わせ用の標準的なname、email、messageフォームであり、返信に最低限必要な情報が得られるよう、フィールド数を最小限に抑えることを目的としています。

営業およびデモのリクエスト

会社名フィールドと件名セレクトを追加して拡張し、訪問者がどの宛先に送るべきか迷うことなく問い合わせをルーティングできます。

サポートリクエスト

カテゴリセレクトと、必要に応じた注文番号またはアカウント参照をペアにすることで、最初の返信で「詳細を教えてください」というやり取りを省き、すぐに有益な回答ができます。

フィードバックとバグレポート

メッセージフィールドと任意のメールアドレス入力欄。メールアドレスを任意にすることで、受け取れるレポートの数が意味のあるレベルで増加します。

Accessibility

Customization

Props

PropTypeDefaultDescription
onSubmit(data: FormData) => Promise<void>-バリデーション通過後にフォームデータとともに呼び出されます。同じフォームから異なるエンドポイントにPOSTできるよう、ネットワーク呼び出しはコンポーネントの外側に保持してください。
successMessagestring"Thanks - we will reply shortly."送信成功後、politeなライブリージョンでアナウンスされ、視覚障害のあるユーザーだけでなくスクリーンリーダーユーザーにも確認メッセージが伝わります。

Why Use These お問い合わせフォームs?

地味ながら重要な部分をきちんと押さえています。idで紐づけられたラベル、autocompleteトークン、エラーに対するaria-describedby、そして二重送信防止機能は、「誰にとっても機能するフォーム」と「それを構築した本人にしか機能しないフォーム」の決定的な違いとなります。

バリデーションは送信時に実行され、入力中のメールアドレスに対して「無効」とエラー表示し続けるようなことはせず、フィールドごとにクリアされます(この挙動が、多くのフォームを使いづらく感じさせています)。

フォームライブラリはありません。3つのフィールドに対しては、スキーマバリデーターやフォームの状態管理パッケージのほうがフォーム本体よりも設定が多くなり、アクセシビリティの配線は依然として開発者に委ねられます。

Tips

Frequently Asked Questions