नए टूल्स जोड़े गए! देखें क्या नया है

होम/कंपोनेंट्स/Contact Form

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

कॉन्टैक्ट फॉर्म वह सबसे आम तरीका है जिससे कोई विज़िटर्स साइट के ओनर तक पहुँचता है, और उन कुछ जगहों में से एक है जहाँ एक छोटी सी इम्प्लीमेंटेशन की गलती की कीमत सीधे एक मैसेज खोने के रूप में चुकानी पड़ती है। हर अतिरिक्त आवश्यक फ़ील्ड, हर अस्पष्ट एरर, और हर ऐसा इनपुट जिसे ऑटोफ़िल पहचान नहीं सकता, ठीक उसी समय फ्रिक्शन पैदा करता है जब किसी ने संपर्क करने का फैसला किया हो।

फंडामेंटल्स बहुत आकर्षक नहीं होते और अक्सर छोड़ दिए जाते हैं। हर इनपुट को id द्वारा जुड़े हुए एक असली लेबल एलिमेंट की ज़रूरत होती है, न कि उसकी जगह लेने वाले एक प्लेसहोल्डर की-टाइपिंग शुरू होने पर प्लेसहोल्डर गायब हो जाते हैं, जिससे अपनी जगह भूलने वाले लोगों के पास फ़ील्ड का मतलब वापस पाने का कोई तरीका नहीं रहता, और स्क्रीन रीडर द्वारा भी इन्हें भरोसेमंद तरीके से अनाउंस नहीं किया जाता। ऑटोमैटिक कारणों से ऑटोमैटिक एट्रिब्यूट्स मायने रखते हैं: इनकी मदद से ब्राउज़र एक टैप में नाम और ईमेल भर देता है, और इनके बिना व्यक्ति को फ़ोन कीबोर्ड पर दोनों चीज़ें हाथ से टाइप करनी पड़ती हैं।

वैलिडेशन वह जगह है जहाँ अधिकांश फॉर्म निराशाजनक हो जाते हैं। हर कीस्ट्रोक पर वैलिडेशन करने का मतलब किसी को यह बताना है कि उनका ईमेल अमान्य है जबकि वे अभी तीसरे अक्षर पर हैं। केवल सबमिट करने पर वैलिडेशन करना, और फिर बिना किसी स्पष्टीकरण के केवल फ़ील्ड को लाल रंग में हाइलाइट करना, उन्हें अनुमान लगाने पर मजबूर कर देता है। जो बीच का रास्ता काम करता है वह है प्रति फ़ील्ड एक विशिष्ट संदेश के साथ सबमिट करने पर वैलिडेशन करना, और फिर उस फ़ील्ड को ठीक करते ही दोबारा वैलिडेट करना ताकि ठीक होने पर एरर हट जाए।

सबमिट स्टेट दिखने से ज़्यादा मायने रखती है। जो बटन कोई फ़ीडबैक नहीं देता वह दूसरे क्लिक को आमंत्रित करता है, और बिना सुरक्षा वाले फॉर्म पर दूसरा क्लिक डुप्लीकेट मैसेज भेजता है। सबमिशन के दौरान बटन को अक्षम (disable) करना और उसका लेबल बदलना इसे संभालता है, और एक पोलाइट लाइव रीजन अंतिम कन्फर्मेशन को उन लोगों तक पहुँचाता है जो मैसेज को उभरते हुए नहीं देख सकते।

Anatomy

Field group
एक लेबल, उसका कंट्रोल, और उसका हिंट या एरर मैसेज, जिन्हें एक साथ रैप किया गया है ताकि फ़ील्ड के बीच की स्पेसिंग सभी फ़ील्ड्स में एक जैसी बनी रहे।
Label
इनपुट के id से मेल खाने वाले for एट्रिब्यूट वाला एक असली लेबल एलिमेंट। यही वह चीज़ है जो लेबल को क्लिक करने योग्य बनाती है और इनपुट को एक एसेसिबल नाम देती है।
Control
इनपुट या textarea, जिसमें फॉर्म डेटा के लिए एक name, सही मोबाइल कीबोर्ड के लिए एक type, और ब्राउज़र फिल के लिए एक ऑटोमैटिक टोकन होता है।
Hint
aria-describedby से जुड़ा वैकल्पिक सपोर्टिंग टेक्स्ट, जिसका उपयोग किसी भी ऐसी चीज़ के लिए किया जाता है जो अन्यथा प्लेसहोल्डर में जाती।
Error message
फ़ील्ड विफल होने पर हिंट की जगह लेता है। aria-describedby के साथ भी जुड़ा हुआ है, और कंट्रोल पर aria-invalid के साथ जोड़ा गया है।
Submit button
एक असली सबमिट बटन ताकि Enter दबाने पर फॉर्म सबमिट हो जाए। भेजने के दौरान अक्षम (disabled), एक ऐसे लेबल के साथ जो वर्तमान स्थिति को दर्शाता है।
Status region
सफलता का संदेश ले जाने वाला एक पोलाइट लाइव रीजन, ताकि कन्फर्मेशन को स्क्रीन पर केवल दिखने के बजाय अनाउंस किया जाए।

How to Use

  1. 1

    फॉर्म कॉपी करें

    नेटिव ब्राउज़र वैलिडेशन पर भरोसा करने के लिए HTML वर्ज़न लें, या कस्टम संदेशों और एक नियंत्रित सबमिट स्टेट के लिए React वर्ज़न लें।

  2. 2

    अपना एंडपॉइंट कनेक्ट करें

    कमेंट किए गए फेच कॉल को अपने API रूट, फॉर्म सर्विस या सर्वर एक्शन से बदलें। फॉर्म डेटा पहले ही एक FormData ऑब्जेक्ट के रूप में एकत्र हो चुका है।

  3. 3

    फ़ील्ड एडजस्ट करें

    आवश्यकतानुसार फ़ील्ड ग्रुप जोड़ें या हटाएँ। प्रत्येक को एक मेल खाने वाले label for और input id जोड़े की आवश्यकता होती है - वह कनेक्शन ही फ़ील्ड को उपयोग करने योग्य बनाता है।

  4. 4

    ऑटोमैटिक टोकन सेट करें

    मानक वैल्यूज़ का उपयोग करें: name, email, tel, organization। इनसे ब्राउज़र फोन पर एक ही टैप में फॉर्म भर पाता है।

  5. 5

    रिस्पॉन्स को हैंडल करें

    सफल होने पर स्टेटस को sent पर सेट करें और विफलता पर एक विशिष्ट मैसेज दिखाएं। एक विफल सेंड जो सफल जैसा दिखता है, वह त्रुटि से भी बदतर है।

Features

हर फील्ड पर असली लेबल्स

हर लेबल id से जुड़ा होता है, जिससे यह क्लिक करने योग्य बनता है और इनपुट को एक एक्सेसिबल नाम देता है - न कि कोई प्लेसहोल्डर जो अपना काम ठीक से न कर पाए।

त्रुटियों (Errors) को उनके फील्ड्स से जोड़ा गया है

मैसेजेस को control पर aria-invalid के साथ aria-describedby के माध्यम से जोड़ा जाता है, ताकि जब फील्ड पर फोकस किया जाए तो त्रुटि की घोषणा (announce) हो, न कि वह केवल दिखाई दे।

ऑटोमैटिक कम्पलीट (Autocomplete) सपोर्ट

हर फील्ड पर मानक टोकन ब्राउज़र और पासवर्ड मैनेजर को एक ही कार्रवाई में फॉर्म भरने की अनुमति देते हैं, जो एक टैप और अंगूठे से टाइप किए गए पैराग्राफ के बीच का अंतर है।

सुरक्षित (Guarded) सबमिट

सेंडिंग के दौरान बटन डिसएबल हो जाता है और इसका लेबल बदल जाता है, जिससे डबल-क्लिक रुकता है जो डुप्लीकेट मैसेज भेज सकता है।

घोषित (Announced) कन्फर्मेशन

सफलता का मैसेज एक polite लाइव रीजन में रहता है, ताकि यह चुपचाप प्रकट होने के बजाय स्क्रीन रीडर उपयोगकर्ताओं तक पहुँचे।

सही मोबाइल कीबोर्ड

इनपुट टाइप्स प्रति फील्ड सेट किए जाते हैं, इसलिए ईमेल फील्ड लोगों को इसके लिए परेशान करने के बजाय at चिह्न (@) वाला कीबोर्ड लाता है।

Common Uses

साइट संपर्क (Contact) पेज

सामान्य पूछताछ के लिए मानक name, email, और message फॉर्म, जहाँ लक्ष्य कम से कम ऐसे फील्ड्स रखना है जो फिर भी आपको उपयोगी रूप से उत्तर देने दें।

सेल्स और डेमो अनुरोध

एक कंपनी फील्ड और एक सब्जेक्ट सिलेक्ट के साथ विस्तारित, जो आगंतुक को यह अनुमान लगाए बिना पूछताछ को रूट करता है कि किस इनबॉक्स में लिखना है।

सपोर्ट अनुरोध

एक कैटेगरी सिलेक्ट और एक वैकल्पिक ऑर्डर या अकाउंट रेफरेंस के साथ युग्मित, ताकि पहला उत्तर विवरण के अनुरोध के बजाय उपयोगी हो सके।

फीडबैक और बग रिपोर्ट्स

वैकल्पिक ईमेल के साथ एक मैसेज फील्ड, जहाँ ईमेल को वैकल्पिक बनाने से प्राप्त रिपोर्ट्स की संख्या सार्थक रूप से बढ़ जाती है।

Accessibility

Customization

Props

PropTypeDefaultDescription
onSubmit(data: FormData) => Promise<void>-वैलिडेशन पास होने के बाद फॉर्म डेटा के साथ कॉल किया जाता है। नेटवर्क कॉल को कॉम्पोनेन्ट से बाहर रखें ताकि एक ही फॉर्म को अलग-अलग एंडपॉइंट्स पर पोस्ट किया जा सके।
successMessagestring"Thanks - we will reply shortly."सफलतापूर्वक सबमिट होने के बाद एक पोलाइट लाइव रीजन के माध्यम से अनाउंस किया जाता है, ताकि कन्फर्मेशन स्क्रीन रीडर यूज़र्स के साथ-साथ देख सकने वाले यूज़र्स तक भी पहुँचे।

Why Use These Contact Forms?

यह अनारकली (unglamorous) भागों को सही करता है। id द्वारा जुड़े लेबल्स, ऑटोपाइलट टोकन, त्रुटियों पर aria-describedby, और एक सुरक्षित सबमिट उस फॉर्म के बीच का अंतर है जो सभी के लिए काम करता है और उस फॉर्म के बीच जो केवल उसे बनाने वाले के लिए काम करता है।

वैलिडेशन सबमिट पर चलता है और प्रति फील्ड साफ़ होता है, न कि ईमेल के टाइप होने के दौरान ही उसे अमान्य के रूप में फ्लैग करना - यह वह व्यवहार है जो कई फॉर्म्स को शत्रुतापूर्ण महसूस कराता है।

कोई फॉर्म लाइब्रेरी नहीं। तीन फ़ील्ड्स के लिए, एक स्कीमा वैलिडेटर और फॉर्म स्टेट पैकेज फॉर्म से ज़्यादा कॉन्फ़िगरेशन मांगते हैं, और वे एसेसिबिलिटी की वायरिंग फिर भी आपके लिए छोड़ देते हैं।

Tips

Frequently Asked Questions