เพิ่มเครื่องมือใหม่แล้ว! ดูสิ่งที่มาใหม่

หน้าแรก/คอมโพเนนต์/Contact Form

Contact Form examples

แบบฟอร์มติดต่อที่ติดป้ายกำกับพร้อมการตรวจสอบความถูกต้องแบบ native, ข้อความแสดงข้อผิดพลาดแบบ inline และสถานะ submit ที่เข้าถึงได้

แบบฟอร์มติดต่อที่มีป้ายกำกับมองเห็นได้, ข้อความ validation แบบ inline และสถานะ submit ที่เข้าถึงได้ ทุกฟิลด์เชื่อมโยงกับป้ายกำกับอย่างถูกต้อง และข้อความแสดงข้อผิดพลาดถูกผูกไว้ผ่าน aria-describedby เพื่อให้มีการประกาศเสียงแทนที่จะแสดงผลเพียงอย่างเดียว

variant แรกจะตรวจสอบความถูกต้องตอน submit และเรนเดอร์ข้อความผิดพลาดรายฟิลด์ ส่วนอันอื่นๆ เป็น static markup ที่พึ่งพาการ validation แบบ native ของ browser

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 Contact Form?

แบบฟอร์มติดต่อคือวิธีที่พบบ่อยที่สุดที่ผู้เยี่ยมชมจะติดต่อเจ้าของเว็บไซต์ และเป็นหนึ่งในไม่กี่จุดที่ความผิดพลาดเพียงเล็กน้อยในการใช้งานทำให้สูญเสียข้อความไป ทุกฟิลด์บังคับที่เพิ่มขึ้น, ทุกข้อความ error ที่ไม่ชัดเจน และทุก input ที่ autocomplete จำไม่ได้ ล้วนสร้าง friction ในวินาทีที่ใครสักคนตัดสินใจจะติดต่อเข้ามา

พื้นฐานเป็นสิ่งที่ไม่หวือหวาแต่ถูกมองข้ามบ่อยครั้ง input ทุกตัวต้องการ element label จริงๆ ที่เชื่อมด้วย id ไม่ใช่ placeholder ที่มาทำหน้าที่แทน - placeholder จะหายไปเมื่อเริ่มพิมพ์ ทำให้คนที่ลืมตำแหน่งไม่มีทางรู้ความหมายเดิมของฟิลด์ได้ และ screen reader ก็ไม่สามารถประกาศค่าได้อย่างน่าเชื่อถือ Attribute autocomplete ก็สำคัญด้วยเหตุผลทางปฏิบัติเดียวกัน: ด้วยสิ่งนี้ browser จะกรอกชื่อและอีเมลได้ในการแตะครั้งเดียว และถ้าไม่มี ผู้ใช้จะต้องพิมพ์ทั้งสองอย่างด้วยมือบนคีย์บอร์ดมือถือ

Validation คือจุดที่แบบฟอร์มส่วนใหญ่น่าหงุดหงิด การ validate ทุกครั้งที่กดปุ่มพิมพ์หมายถึงการบอกใครบางคนว่าอีเมลของพวกเขาไม่ถูกต้องในขณะที่พวกเขายังพิมพ์อยู่แค่ตัวอักษรที่สาม การ validate เฉพาะตอน submit แล้วไฮไลต์ฟิลด์เป็นสีแดงโดยไม่มีคำอธิบายใดๆ จะทำให้พวกเขาต้องเดา ทางสายกลางที่ใช้งานได้คือการ validate ตอน submit พร้อมข้อความระบุเฉพาะสำหรับแต่ละฟิลด์ จากนั้นทำการ revalidate ฟิลด์นั้นๆ เมื่อมีการแก้ไข เพื่อให้ error หายไปเมื่อได้รับการแก้ไขแล้ว

สถานะ submit สำคัญกว่าที่ตาเห็น ปุ่มที่ไม่มี feedback ใดๆ จะเชิญชวนให้คลิกซ้ำ และการคลิกซ้ำบน form ที่ไม่มีการป้องกันจะส่งข้อความซ้ำ การ disable ปุ่มระหว่างการส่งและเปลี่ยน label จะช่วยจัดการเรื่องนี้ได้ และ polite live region จะส่งข้อความยืนยันไปยังผู้ที่ไม่สามารถมองเห็นข้อความที่ปรากฏขึ้น

Anatomy

Field group
Label, control และข้อความ hint หรือ error ถูกห่อรวมกันเพื่อให้ระยะห่างระหว่างทั้งสามส่วนมีความสม่ำเสมอในทุกฟิลด์
Label
Element label จริงๆ ที่มี attribute for ตรงกับ id ของ input นี่คือสิ่งที่ทำให้ label สามารถคลิกได้และให้ชื่อที่เข้าถึงได้แก่ input
Control
Input หรือ textarea ที่มี name สำหรับข้อมูล form, type สำหรับคีย์บอร์ดมือถือที่ถูกต้อง และ autocomplete token สำหรับการเติมข้อมูลอัตโนมัติของ browser
Hint
ข้อความช่วยเหลือเสริมที่เชื่อมต่อด้วย aria-describedby ใช้สำหรับเนื้อหาใดๆ ที่ปกติควรจะใส่ไว้ใน placeholder
Error message
แทนที่ hint เมื่อฟิลด์เกิดข้อผิดพลาด เชื่อมต่อด้วย aria-describedby และใช้งานคู่กับ aria-invalid บน control
Submit button
ปุ่ม submit จริงๆ เพื่อให้ปุ่ม Enter สามารถส่ง form ได้ จะถูก disabled ระหว่างการส่ง พร้อม label ที่สะท้อนสถานะปัจจุบัน
Status region
Polite live region ที่แสดงข้อความสำเร็จ เพื่อให้มีการประกาศข้อความยืนยันแทนที่จะปรากฏบนหน้าจอเพียงอย่างเดียว

How to Use

  1. 1

    Copy the form

    ใช้เวอร์ชัน HTML เพื่อพึ่งพาการ validation แบบ native ของ browser หรือเวอร์ชัน React สำหรับข้อความกำหนดเองและสถานะ submit แบบควบคุมได้

  2. 2

    Connect your endpoint

    แทนที่คำสั่ง fetch ที่ถูกคอมเมนต์ด้วย API route, form service หรือ server action ของคุณ ข้อมูล form ถูกรวบรวมเป็น FormData object เรียบร้อยแล้ว

  3. 3

    Adjust the fields

    เพิ่มหรือลบ field group ตามต้องการ แต่ละอันต้องมีคู่ของ label for และ input id ที่ตรงกัน - การเชื่อมต่อนี้คือสิ่งที่ทำให้ฟิลด์ใช้งานได้

  4. 4

    Set autocomplete tokens

    ใช้ค่ามาตรฐาน: name, email, tel, organization ค่าเหล่านี้ช่วยให้เบราว์เซอร์สามารถกรอกฟอร์มได้ด้วยการแตะเพียงครั้งเดียวบนมือถือ

  5. 5

    จัดการกับผลลัพธ์ที่ตอบกลับ

    กำหนดสถานะเป็น sent เมื่อสำเร็จและแสดงข้อความเฉพาะเมื่อเกิดความผิดพลาด การส่งที่ไม่สำเร็จแต่หน้าตาเหมือนกับส่งสำเร็จนั้นแย่ยิ่งกว่าการแสดง error

Features

มี label จริงสำหรับทุกฟิลด์

แต่ละ label เชื่อมโยงด้วย id ทำให้สามารถคลิกได้และทำให้ input มีชื่อที่เข้าถึงได้ (accessible name) ไม่ใช่ใช้ placeholder มาทำหน้าที่แทนอย่างไม่เหมาะสม

เชื่อมโยง error กับฟิลด์ของตนเอง

ข้อความจะถูกเชื่อมโยงผ่าน aria-describedby พร้อมกับ aria-invalid บนตัวคอนโทรล ทำให้มีการประกาศ error เมื่อฟิลด์นั้นได้รับ focus แทนที่จะมองเห็นแค่ตาเปล่า

รองรับ Autocomplete

Token มาตรฐานในทุกฟิลด์ช่วยให้เบราว์เซอร์และโปรแกรมจัดการรหัสผ่านกรอกฟอร์มได้ในคลิกเดียว ซึ่งคือความแตกต่างระหว่างการแตะหนึ่งครั้งกับการมานั่งพิมพ์ด้วยนิ้วโป้งเป็นพารากราฟ

ป้องกันการกด submit ซ้ำ

ปุ่มจะถูก disable ระหว่างกำลังส่งและข้อความปุ่มจะเปลี่ยนไป ช่วยป้องกันการดับเบิลคลิกที่อาจส่งข้อความซ้ำกัน

ประกาศการยืนยันเมื่อสำเร็จ

ข้อความสำเร็จจะอยู่ใน live region แบบ polite ทำให้โปรแกรมอ่านหน้าจอ (screen reader) อ่านออกเสียงให้ผู้ใช้ฟังแทนที่จะปรากฏขึ้นมาเงียบๆ

คีย์บอร์ดมือถือที่ถูกต้อง

กำหนด input type แยกตามแต่ละฟิลด์ ดังนั้นฟิลด์อีเมลจึงแสดงคีย์บอร์ดที่มีเครื่องหมาย @ ขึ้นมาทันที โดยที่ผู้ใช้ไม่ต้องเสียเวลาหา

Common Uses

หน้าติดต่อเว็บไซต์

ฟอร์มสำหรับชื่อ อีเมล และข้อความมาตรฐานสำหรับการติดต่อทั่วไป ซึ่งเป้าหมายคือการมีจำนวนฟิลด์ให้น้อยที่สุดแต่ยังช่วยให้คุณตอบกลับได้อย่างมีประโยชน์

คำขอฝ่ายขายและขอเดโม

ขยายเพิมด้วยฟิลด์บริษัทและตัวเลือกหัวข้อ ช่วยจัดเส้นทางคำถามโดยที่ผู้ใช้ไม่ต้องเดาว่าควรส่งไปที่กล่องข้อความใด

คำขอรับการสนับสนุน (Support)

จับคู่กับตัวเลือกหมวดหมู่และเลขอ้างอิงคำสั่งซื้อหรือบัญชีที่เป็น optional ทำให้การตอบกลับครั้งแรกมีประโยชน์ทันที ไม่ใช่แค่การวนกลับมาขอรายละเอียดเพิ่ม

ข้อเสนอแนะและรายงานบั๊ก

ฟิลด์ข้อความพร้อมอีเมลที่เป็นทางเลือก (optional) การทำให้อีเมลเป็นทางเลือกจะช่วยเพิ่มจำนวนรายงานที่ได้รับอย่างมีนัยสำคัญ

Accessibility

Customization

Props

PropTypeDefaultDescription
onSubmit(data: FormData) => Promise<void>-เรียกใช้งานด้วยข้อมูล form เมื่อผ่านการ validation แล้ว แยก network call ให้อยู่ภายนอกคอมโพเนนต์ เพื่อให้ form เดียวกันสามารถส่งไปยัง endpoint ที่ต่างกันได้
successMessagestring"Thanks - we will reply shortly."ประกาศผ่าน polite live region หลังจาก submit สำเร็จ เพื่อให้ผู้ใช้ screen reader ได้รับข้อความยืนยันเช่นเดียวกับผู้มองเห็น

Why Use These Contact Forms?

มันทำส่วนที่ไม่น่าดึงดูดใจให้ถูกต้อง: label ที่เชื่อมโยงด้วย id, autocomplete token, aria-describedby บน error และปุ่ม submit ที่มีการป้องกัน คือความแตกต่างระหว่างฟอร์มที่ใช้งานได้จริงสำหรับทุกคนกับฟอร์มที่ใช้งานได้เฉพาะกับคนที่สร้างมันขึ้นมา

การ Validation จะทำงานตอนกด submit และจะเคลียร์ทีละฟิลด์ แทนที่จะแสดง error ว่าอีเมลไม่ถูกต้องตั้งแต่ตอนที่กำลังพิมพ์อยู่ ซึ่งเป็นพฤติกรรมที่ทำให้ฟอร์มส่วนใหญ่ดูเป็นมิตรน้อยเหลือเกิน

ไม่มีไลบรารีจัดการฟอร์ม สำหรับฟิลด์สามฟิลด์ ตัวตรวจสอบ Schema และแพ็กเกจจัดการสถานะฟอร์มจะเพิ่มความซับซ้อนมากกว่าตัวฟอร์มเอง และยังคงต้องให้คุณจัดการเรื่อง Accessibility ด้วยตัวเองอยู่ดี

Tips

Frequently Asked Questions