Skip to content

Contact form with validation in WordPress

A form whose errors appear clearly under each field, at the right time.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

formulaire-contact-valide.php
<?php
/**
 * Plugin Name: Forge, Formulaire de contact avec validation
 * Description: Un formulaire dont les erreurs s'affichent clairement sous chaque champ, au bon moment. Ajoute la composition « Formulaire de contact avec validation » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-formulaire-contact-valide
 *
pattern.html
<!-- wp:html -->
<form class="vform" data-validate novalidate action="#envoi">
        <div class="vform__field">
          <label for="contact-name">Nom</label>
          <input id="contact-name" type="text" name="name" autocomplete="name" required minlength="2" aria-describedby="contact-name-error">
          <p class="vform__error" id="contact-name-error"></p>
        </div>
        <div class="vform__field">
          <label for="contact-email">Adresse e-mail</label>
          <input id="contact-email" type="email" name="email" autocomplete="email" required aria-describedby="contact-email-error">
          <p class="vform__error" id="contact-email-error"></p>
        </div>
script.js
(() => {
  if (!document.querySelector(".vform")) return;
  const messages = {
    valueMissing: "Ce champ est obligatoire.",
    typeMismatch: "Saisissez une adresse e-mail valide, par exemple nom@exemple.fr.",
    tooShort: (field) => `Écrivez au moins ${field.minLength} caractères.`,
  };

  function errorFor(field) {
    const { validity } = field;
    if (validity.valueMissing) return messages.valueMissing;
    if (validity.typeMismatch) return messages.typeMismatch;
style.css
:root {
  --form-bg: #ffffff;
  --form-line: #94a3b8;
  --form-text: #0f172a;
  --form-muted: #475569;
  --form-focus: #1d4ed8;
  --form-error: #b91c1c;
  --form-button: #1d4ed8;
  --form-button-text: #ffffff;
  --demo-formulaire-contact-valide-bg: #f1f5f9;
  --demo-formulaire-contact-valide-text: #0f172a;
}

Free component: copy it freely, including for commercial projects.

Forms and inputs components in WordPress
  • A quote request form for tradespeople: the customer describes the work, picks a start date range and attaches up to six site photos, each with a preview, its size and a button to remove it.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A quote request form whose labels, hints and error messages are full French sentences, still readable at 320 px: nothing overflows, nothing is cut off, and each error is tied to its field.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Visitors pick a package and tick options: the pre-tax total, VAT and total including tax update live, and the summary is sent with the contact form.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A short NPS survey: a 0 to 10 score as radio buttons, then a follow-up question adapted to whether the customer is a detractor, passive or promoter.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • An honest unsubscribe page: address prefilled from the link, choice between stopping everything or receiving less, optional reason and an undo.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A callback request form: French phone number checked and formatted, topic, working day and time slot, with explicit consent to the call.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component