Skip to content

Contact Form with Validation

Bootstrap 6 contact form with floating labels, CSS-only validation via data-bs-validate, a character counter, consent check and a simulated send with success alert.

Also known as: validated contact form, contact us form, floating label contact form, contact form with consent, message form with character counter

Contact Form with Validation live demo

Open in new tab

Bootstrap 6 code (HTML, CSS, JS)

A warm, two-column contact form for Bootstrap 6 with floating labels and validation that runs almost entirely in CSS. The left column explains response times and how the email address is used; the right column holds the form with name, email, subject, optional company, message and a consent checkbox. It suits company websites, SaaS support pages and agency portfolios.

How it works

The <form class="cfv-form"> carries Bootstrap 6's data-bs-validate attribute together with novalidate. Bootstrap then styles fields with the :user-invalid pseudo-class: a field turns red and its .invalid-feedback sibling appears only after the visitor has interacted with it, never on first load. A submit attempt marks every field as touched, so pressing "Send message" on an empty form highlights all required fields at once. Each field uses .form-floating with the <label> before the control, as Bootstrap 6 expects.

The script adds three small things. It updates .cfv-count-n while typing in .cfv-message (limit 600, highlighted with .is-near past 90%). On submit it mirrors validity into aria-invalid, writes a summary such as "Please fix 3 fields above." into the .cfv-status live region and focuses the first invalid control. When everything is valid, the button shows a .spinner-border, then the form resets and the .cfv-success alert (alert theme-success) appears with the sender's first name and email.

Customization

  • --cfv-accent and --cfv-accent-dark on .cfv-root: change the orange of the button, checkbox, focus ring and icons; the scoped --bs-primary-* overrides follow them.
  • --cfv-sand, --cfv-line and --cfv-ink on .cfv-root: adjust the diagonal background, borders and heading color.
  • minlength and maxlength on #cfv-message: change the message limits; the counter reads maxlength automatically.
  • <option> elements in #cfv-subject: list your own topics.
  • Use data-bs-validate="valid" instead of data-bs-validate to also show a green valid state.
  • Replace the setTimeout in the submit handler with fetch('/api/contact', { method: 'POST', body: new FormData(form) }).

Use cases

  • Business websites: a contact form with floating labels and validation that needs no JavaScript
  • Support and inquiry pages: a character counter guides the length of the message
  • Privacy-conscious sites: a consent checkbox is required before sending
  • Landing pages: a simulated send shows a success alert once the form is valid

Accessibility

Every control has a real <label>, and each error message is linked through aria-describedby. Invalid fields get aria-invalid="true", the summary line is a role="status" region and the character counter is aria-live="polite". After a successful send, focus moves to the success alert; its close button returns focus to the name field. Focus rings use the accent color at a readable contrast.

Bootstrap 6 notes

Bootstrap 5 needed JavaScript to add .was-validated; Bootstrap 6 replaces that with data-bs-validate and :user-invalid. .form-select is gone, so the subject uses .form-control on a <select>. Checkboxes are a plain .check inside .form-field, alerts use alert theme-success, and btn-primary becomes btn-solid theme-primary. Built on Bootstrap 6.0.0-alpha.1, so APIs may still change.

Contact Form with Validation at a glance

Framework
Bootstrap 6
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Bootstrap 6, Bootstrap Icons
Layout
Responsive, works on mobile
Category
Contact Forms
License
Free for personal and commercial use

Questions about this template

Can I use the Contact Form with Validation code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Bootstrap 6 and Bootstrap Icons from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/css/bootstrap.min.css">
  • <script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/js/bootstrap.bundle.min.js"></script>
  • <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

Built for Bootstrap 6.0.0-alpha.1. Bootstrap 6 ships as an ES module, so its script and this component's JavaScript load with type="module". Alpha releases can still change before the stable version.

Contact Form with Validation – free Bootstrap 6 contact form template
Contact Form with Validation · free Bootstrap 6 contact form template

More Contact Forms

Contact Form with Map and Details – free Bootstrap 6 contact form template

Contact Form with Map and Details

Dark Bootstrap 6 contact section with an OpenStreetMap embed, address, phone, email and live opening hours, plus a validated form and Toast confirmation.

Contact Form Card – free Tailwind CSS contact form template

Contact Form Card

Tailwind CSS contact form card with a topic select, live character counter and meter, inline validation and a simulated sending state.

Contact Form with Validation – free Bootstrap 5 contact form template

Contact Form with Validation

Bootstrap 5 contact form with floating labels, client-side validation, a character counter, a success message and a contact details panel.