Skip to content
DevFlash

Junior

Sign-up form with accessible errors

Validate on submit, tie every error to its field and send focus where the person needs to fix something.

FreeJunior~30 min

This challenge is under editorial review.

Why it shows up in interviews

Forms are where front-end work meets real users, and a take-home or live coding round often includes one. Showing a red message is the easy part; making sure everyone can find and fix the error is what gets graded.

You will tie each message to its input with aria-describedby, mark invalid fields, keep the submit from firing with errors and send focus to the first field that needs fixing, all in plain React, no form library.

What you will build

  1. Fields labelled "Email" and "Password", and a "Create account" submit button.
  2. Empty email shows "Email is required"; an email without user, @ and domain shows "Enter a valid email".
  3. A password shorter than 8 characters shows "Password must be at least 8 characters".
  4. An invalid field has aria-invalid="true" and aria-describedby pointing to its error.
  5. With errors, onSubmit is not called and focus moves to the first invalid field.
  6. When valid, onSubmit is called once with the email trimmed and the password as typed.

Out of scope: Validating while typing. A form library. Plain React is enough here.

How you will be graded

Passing the tests is the start. In the follow-up conversation, a good explanation covers:

  • Explains why focus goes to the first invalid field.
  • Says how errors are announced to assistive technology.
  • Justifies controlled versus uncontrolled inputs here.

Starter code

/src/SignupForm.tsx

type Values = { email: string; password: string };
export function SignupForm({ onSubmit }: { onSubmit: (values: Values) => void }) {
// implemente aqui
return (
<form>
<label htmlFor="email">Email</label>
<input id="email" type="email" />
<label htmlFor="password">Password</label>
<input id="password" type="password" />
<button type="submit">Create account</button>
</form>
);
}
See the tests that will run /src/SignupForm.test.tsx
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { SignupForm } from "./SignupForm";
const email = () => screen.getByLabelText("Email");
const password = () => screen.getByLabelText("Password");
const fill = (field: HTMLElement, value: string) => fireEvent.change(field, { target: { value } });
const submit = () => fireEvent.click(screen.getByRole("button", { name: "Create account" }));
describe("SignupForm", () => {
it("submits once with the trimmed email", () => {
const onSubmit = jest.fn();
render(<SignupForm onSubmit={onSubmit} />);
fill(email(), " [email protected] ");
fill(password(), "12345678");
submit();
expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit).toHaveBeenCalledWith({ email: "[email protected]", password: "12345678" });
});
it("does not submit when the fields are empty", () => {
const onSubmit = jest.fn();
render(<SignupForm onSubmit={onSubmit} />);
submit();
expect(onSubmit).not.toHaveBeenCalled();
expect(screen.getByText("Email is required")).toBeInTheDocument();
expect(screen.getByText("Password must be at least 8 characters")).toBeInTheDocument();
});
it("rejects an email without a domain", () => {
render(<SignupForm onSubmit={jest.fn()} />);
fill(email(), "ana@");
submit();
expect(screen.getByText("Enter a valid email")).toBeInTheDocument();
});
it("links each error to its field", () => {
render(<SignupForm onSubmit={jest.fn()} />);
submit();
expect(email()).toHaveAttribute("aria-invalid", "true");
const errorId = email().getAttribute("aria-describedby") ?? "";
expect(document.getElementById(errorId)).toHaveTextContent("Email is required");
});
it("moves focus to the first invalid field", () => {
render(<SignupForm onSubmit={jest.fn()} />);
fill(email(), "[email protected]");
fill(password(), "short");
submit();
expect(password()).toHaveFocus();
});
it("clears an error once the field is fixed", () => {
const onSubmit = jest.fn();
render(<SignupForm onSubmit={onSubmit} />);
submit();
fill(email(), "[email protected]");
fill(password(), "12345678");
submit();
expect(screen.queryByText("Email is required")).not.toBeInTheDocument();
expect(email()).toHaveAttribute("aria-invalid", "false");
expect(onSubmit).toHaveBeenCalledTimes(1);
});
});

Hints

Open only if you get stuck. Each one gives away a little more.

Hint 1

Write validate(values) as a pure function that returns an errors object. Rendering and focus then read from it.

Hint 2

noValidate on the form turns off the browser's own bubbles, so your messages are the only ones.

References