You write the code. This guide explains the ideas, the order of work and the traps. It never hands you the finished code, because building it yourself is the point.
What you're building
The Contact section works. A visitor types their name, email and a message and presses Send. If they left something out or mistyped their email, a short, specific message appears under that field ("Please enter an email like name@example.com") and focus moves to it. If everything's fine, the button changes to "Sending...", and a moment later the form is replaced by "Thanks, I'll reply within a couple of days." Their message is in your inbox. The page never reloaded.
There's no server of your own. A form service receives the submission and emails it to you. This guide uses Formspree. Web3Forms works the same way if you prefer it; only the endpoint and field names differ.
Why it matters
Forms are where users give you data, and where most accessibility and validation bugs live. Using built-in browser validation first and JavaScript only to improve it is called progressive enhancement: the page works at a basic level for everyone, and gets better where JavaScript runs. You'll also make your first network request with fetch and handle the three outcomes every request has: success, a failure response, and no response at all.
Before you start
- How a form submits. A
formwithaction(a URL) andmethod="post"sends every field that has anameto that URL when submitted, then loads whatever page comes back. This works with no JavaScript at all. - Constraint validation. Attributes that make the browser check fields before submitting:
required(must not be empty),type="email"(must look like an email),minlengthandmaxlength. If a check fails, the browser blocks the submit and shows its own bubble. - The validity object. Every input has an
input.validityobject with flags likevalueMissing,typeMismatchandtooShort.form.checkValidity()returnstrueonly if every field passes. You read these flags to write your own messages instead of the browser's. fetchandFormData.fetchmakes an HTTP request from JavaScript and returns a promise.new FormData(form)collects every named field into a bodyfetchcan send.- Live regions. An element with
aria-live="polite"makes screen readers announce text when it changes. That's how a blind user hears "Message sent" without the page reloading.
Build it step by step
Step 1: Create the form endpoint
Sign up at Formspree, create a new form, and copy its endpoint URL, which looks like https://formspree.io/f/abcdwxyz. Set it as your form's action and add method="post". The endpoint is meant to be public, so it's fine in your HTML and on GitHub.
Follow any email-confirmation step the Formspree dashboard asks for, and check its current plan limits there.
Check it: with no JavaScript yet, fill in the form and submit. You should land on a Formspree confirmation page, and the message should arrive in your email.
Step 2: Add the validation attributes
Your fields from the skeleton milestone already have labels and names. Now add the rules:
| Field | name | Attributes |
|---|---|---|
| Name | name | required, autocomplete="name", maxlength="100" |
email | type="email", required, autocomplete="email" | |
| Message | message | required, minlength="10", maxlength="2000" |
Add a hidden spam trap too, called a honeypot: an extra input that real people never see or fill in, but bots do. Formspree documents a field named _gotcha for this. Hide it with CSS and tabindex="-1", plus autocomplete="off".
Check it: try to submit with an empty name. The browser blocks it with its own message. Type "asha@" as the email: blocked again.
Step 3: Take over the error messages with JavaScript
The browser's bubbles vary by browser and vanish quickly. In main.js, find the form and set form.noValidate = true. Doing this from JavaScript, rather than writing novalidate in the HTML, means visitors without JavaScript keep the browser's built-in checks.
Under each field, add an empty p with an id for its error, linked to the input with aria-describedby so screen readers read it with the label. Then, on submit:
on submit:
prevent the default submit
for each field: clear its message
if form.checkValidity() is false:
for each invalid field:
pick a message from field.validity (valueMissing, typeMismatch, tooShort)
show it, set aria-invalid="true" on the field
move focus to the first invalid field
stop
send the form (step 4)Say how to fix it: "Your message needs at least 10 characters" beats "Invalid input".
Check it: submit an empty form. Every field shows its own message, and the cursor lands in the first one.
Step 4: Send it with fetch
When the form is valid, send it without leaving the page. Formspree returns JSON instead of a redirect if you ask for it with an Accept header:
const response = await fetch(form.action, {
method: "POST",
body: new FormData(form),
headers: { Accept: "application/json" },
});Before the request, disable the submit button and change its text to "Sending...". Then handle all three outcomes: response.ok is true (show the thank-you message and reset the form), response.ok is false (show "Something went wrong, please email me at ..." with your address as a link), or fetch itself throws because the visitor is offline (same fallback). Re-enable the button in every case except success.
Check it: send a real message and watch the Network panel. You should see a POST to your endpoint with status 200, and the email should arrive.
Step 5: Announce the result
Put the status message in an element with aria-live="polite" that exists in the page from the start, empty. Live regions announce changes to their content, so an element you create and insert at the last moment is often not announced. Write "Sending...", then "Thanks, your message is on its way", into it.
Check it: in DevTools, set the network to Offline and submit. You should see your friendly failure message, with your email address as a fallback.
Step 6: Clear errors as the user fixes them
Once a field has shown an error, listen for input events on it and re-check just that field, clearing the message as soon as it's valid. Don't show errors while someone is typing a field for the first time. Being told your email is invalid after one letter is annoying.
Check it: submit empty, then type a valid email. That field's message disappears as soon as the email is complete.
Common mistakes
- Writing
novalidatein the HTML. Symptom: with JavaScript off, empty forms get sent. Fix: setform.noValidate = truefrom your script, so only JavaScript users skip the native checks. - Fields with no
nameattribute. Symptom: the email arrives, but the message body is empty. Fix:FormDataand normal submits only include fields that have aname. - Forgetting the
Acceptheader. Symptom: the request is sent and the email arrives, but your code receives HTML, not JSON, and your success message never shows. Fix: sendAccept: application/json. - Not disabling the button. Symptom: an impatient double-click sends you the same message twice. Fix: disable it before
fetch, re-enable on failure. - Trusting
type="email"too much. Symptom: "a@b" passes, because it's a valid address by the standard. Fix: that's fine. Client-side validation is for helping honest users, not for security. The form service is what filters spam.
Say it in the interview
"The form works with no JavaScript at all: native required, type="email" and minlength checks, posting straight to Formspree. JavaScript then enhances it by turning off the native bubbles, showing specific messages from input.validity, and sending with fetch so the page doesn't reload. I handle success, an error response and a network failure, and announce the result in a live region for screen readers. The tradeoff of using a form service instead of my own API is depending on a third party, but it removed a whole server from a static site."
Done when
0/6 checkedTest each one yourself. If one fails, you're not done yet, and that's fine.
Stretch goals
- Add a character counter under the message field that updates as you type and turns your accent colour near the limit.
- Save the unsent message to
sessionStorageas the visitor types, so an accidental refresh doesn't lose it.
Go deeper
The official docs for everything in this step.