Review a license-plate validator from its failure paths first

Review a license-plate validator from its failure paths first

●3 ●53
calendar_today ago • schedule2 min read
— Originally published at chriseugenerodriguez.com

Start with the failure cases

Before reviewing the happy path, inspect what the field refuses to accept. A blank value must produce a required-field message. A value with an undersized middle letter group must produce a format hint, not a vague “invalid” label. Those failures define the interaction contract: users need to know whether they omitted a required value or need to change its structure.

Prerequisites

You need a form control marked as required, a nearby feedback element associated through aria-describedby, and a server endpoint that can independently apply the same acceptance rule. Decide the one plate family your product actually supports before writing a pattern.

Make normalization the implementation decision

The useful choice is not “allow more punctuation” in isolation. It is to parse the meaningful groups, allow documented separators between them, and convert successful input to one stored representation.

For the supported format, the parser recognizes three groups: 1–3 letters, 2–4 letters, and 1–4 digits. Spaces, hyphens, periods, and slashes can separate them. A value like b-ab/123 therefore succeeds, but its result is normalized to B AB 123.

The expected result is twofold. Users are not blocked because their separator differs from the application’s preferred presentation. Meanwhile, later comparisons use one representation, which avoids treating punctuation variants as different plates. While typing, the UI can return one of three concise outcomes: required, format guidance, or confirmation of the canonical value.

Review the proof boundary

The supplied assertions establish the helper’s return contract for a separator variant, missing input, and an invalid middle group. They also establish that the field metadata marks the control as required. This is a focused unit-level proof of parsing, feedback, and normalization.

One limitation remains: it does not reproduce browser-native validation behavior or prove server acceptance. It also does not cover every jurisdiction’s plate syntax. Keep the client rule narrow and explainable, then repeat the authoritative validation and normalization on the server.

Working example

// This client-side helper accepts the documented German-style plate groups:
// city (1–3 letters), letters (2–4), and digits (1–4).
// It tolerates spaces, hyphens, periods, and slashes between groups.
export const plateField = Object.freeze({
  label: 'License plate (required)',
  attributes: Object.freeze({
    required: true,
    // Suitable for an HTML pattern attribute; normalization still supplies the
    // canonical value used by the application.
    pattern: '[A-Za-z]{1,3}\\s*[-./ ]+\\s*[A-Za-z]{2,4}\\s*[-./ ]+\\s*[0-9]{1,4}',
    ariaDescribedBy: 'plate-feedback'
  })
});

const plateGroups = /^\s*([a-z]{1,3})\s*[-./ ]+\s*([a-z]{2,4})\s*[-./ ]+\s*([0-9]{1,4})\s*$/i;

/**
 * Returns a canonical, storage-friendly plate or null when the format is not
 * one of the supported group layouts.
 */
export function normalizePlate(value) {
  if (typeof value !== 'string') return null;

  const match = value.match(plateGroups);
  if (!match) return null;

  return `${match[1].toUpperCase()} ${match[2].toUpperCase()} ${match[3]}`;
}

/**
 * Produces concise inline feedback suitable for updating an aria-describedby
 * message while the person types. Server-side validation must repeat any
 * validation required for accepting a submission.
 */
export function validatePlateInput(value) {
  if (typeof value !== 'string' || value.trim() === '') {
    return {
      valid: false,
      normalized: null,
      feedback: 'Enter a license plate; this field is required.'
    };
  }

  const normalized = normalizePlate(value);
  if (normalized === null) {
    return {
      valid: false,
      normalized: null,
      feedback: 'Use 1–3 letters, 2–4 letters, and 1–4 digits, separated by spaces, hyphens, periods, or slashes.'
    };
  }

  return {
    valid: true,
    normalized,
    feedback: `Plate format looks good: ${normalized}.`
  };
}

Read the full article: https://chriseugenerodriguez.com/blog/validate-required-license-plate-field

Want more #ux deep dives?

Join 6,101+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Local-First: The Browser as the Vault

Pocket Portfolio - Apr 20

AAIF Hits 10 Projects. Its CTO on What to Adopt Now and the Security Gaps Left Open

Tom Smithverified - Oct 23

Cisco's Amy Chang: A Model's "Passport" Doesn't Tell You Where It Actually Came From

Tom Smithverified - Aug 27

What Should a Health Awareness Page Help a Reader Do Next?

WAQAS AHMAD - Nov 13

Python for commerce students, from a former bio kid

rahuldk1105 - Oct 30
chevron_left
1.1k Points • 56 Badges
Santa Monica, CA • chriseugenerodriguez.com
42Posts
0Comments
11Connections
10 years fe

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!