liveBook · Manning

Validation

As explained in Acing the Frontend Interview

Form validation is the checking of submitted or entered values against syntactic, semantic, security, and business constraints.

Form validation checks entered values as part of submission. In an interview, explain both where checks occur and what happens when a value fails: the browser can check inputs before collecting and sending form data, while the server validates received data before processing it. section 2.4

A practical validation answer should distinguish these responsibilities:

  • Use built-in HTML attributes such as required and pattern, or JavaScript or TypeScript logic, for client-side checks. section 2.4
  • Validate received data on the server for integrity and authenticity; client-side checks are not the end of the submission process. section 2.4
  • Make errors understandable and accessible. An input can point to its error element with aria-describedby, while that element uses role="alert" and aria-live="assertive". section 2.4

Validate before submitting a React form

A form action reads name and email from FormData, runs validateName and validateEmail, and returns an error with the entered field values when either check fails. If both pass, it proceeds with the request. This makes the interview follow-up concrete: describe how the user sees an error without losing what they entered, then explain why the server still validates the request. section 2.4

Listing 2.9 Advanced form implementation
const Form: React.FC = () => {
  const [state, formAction, isPending] = useActionState<FormState, FormData>(
    async (prevState, formData: FormData) => {
      const name = formData.get('name') as string; #A
      const email = formData.get('email') as string; #B
      const nameError = validateName(name); #C
      if (nameError) { #D
        return {
          data: null,
          error: nameError,
          fields: {
            name,
            email,
          },
        };
      }
      const emailError = validateEmail(email);  #E
      if (emailError) {
        return {
          data: null,
          error: emailError,
          fields: {
            name,
            email,
          },
        };
      }
      try {
        ...
        return {
          data,
          error: null,
          fields: {
            name,
            email,
          },
        };
      } catch (error) {
        return {
          data: null,
          error: `There was a problem with your submission: ${error}`,
          fields: {
            name,
            email,
          },
        };
      }
    },
    {
      data: null,
      error: null,
      fields: {
        name: '',
        email: '',
      },
    }
  );
  return (
    <form action={formAction}>
      ...
      <label htmlFor="name">Name:</label>
      <input type="text" name="name" defaultValue={state.fields!.name} /> #F
      <label htmlFor="email">Email:</label>
      <input type="email" name="email" defaultValue={state.fields!.email} /> #G
      <input type="submit" value="Submit" disabled={isPending} />
      {state.error && <div className="error">{state.error}</div>} #H
      {state.data && (
        <div className="success"> #I
          Form submitted successfully: ${JSON.stringify(state.data)}
        </div>
      )}
      ...
    </form>
  );
}

The advanced form listing shows the order of field checks and the error state returned with retained values. section 2.4

How Acing the Frontend Interview covers it

How deep it goes
Works through client-side checks, an error-returning React form action, and server-side validation, without detailing server validation rules.
Examples
Uses required and pattern attributes, accessible email-error markup, and a React action checking name and email.
What you can do after
Apply it via HTML attributes or React form logic, and explain why the server also validates submitted data.
How it connects
Connects validation to HTML forms, FormData, accessibility, HTTP requests, and server-side processing.

Also explained in

Compare how 44 books explain validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.