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