Contact Form
UI Coding
Easy
25 min
forms
validation
accessibility
async
Build a "Contact us" form with Name, Email and Message fields. The starter has a fake sendMessage(data) that resolves after about 400 ms.
Requirements
- Validate on submit (in JavaScript, so add
noValidateto the form):- Name is required.
- Email is required and must look like an email.
- Message is required and must be at least 10 characters.
- Show each error under its field. Mark invalid fields with
aria-invalid="true"and link the error witharia-describedby. - On a failed submit, move focus to the first invalid field.
- Once a field has an error, re-validate it as the user types, so the error disappears as soon as it's fixed.
- While sending, disable the submit button and change its text to "Sending…".
- On success, replace the form with a thank-you message ("Thanks, Ada! We'll reply to ada@example.com soon.") in a
role="status"region, with a "Send another message" button that brings back an empty form. - Every field has a visible
<label>.
Follow-up
- Native constraint validation (
required,type="email") or custom JavaScript validation? - Validate on blur, on change, or on submit? What's the best UX?
- How would you handle a server-side validation error (for example "This email is blocked")?
Spotted a mistake or a rights concern? Report an issue