· 2 min read
Form validation with what the browser already has
The CV form checks itself with no library: required on name, surnames, phone and email, type="email", and a pattern for the phone (\+?[0-9\s\(\)\-]{9,20}) and…
What we did
- The CV form checks itself with no library:
requiredon name, surnames, phone and email,type="email", and apatternfor the phone (\+?[0-9\s\(\)\-]{9,20}) and the postal code. - A field with a problem gets a red border (the new
--errortoken) and a message under it. It's checked when I leave it, then on every keystroke until it's right. - Print checks every field first: if something's wrong it says so and focuses the first bad field instead of printing. An import checks what it loaded.
- The fields are data now (
src/lib/projects/cv/inputs.ts): label, type, autocomplete, required, pattern, hint. The form loops over them. --erroris--red-dark/--red-bright: ≥ 5.74:1 in light and ≥ 7.45:1 in dark on every surface. The contrast checker lists it.
What I learned
- The constraint validation API: every field has
validity, one flag per problem (valueMissing,typeMismatch,patternMismatch…).novalidateon the form only stops the browser's bubbles; the fields still check themselves. - My own messages, not
validationMessage: that one comes in the browser's language. focusoutbubbles (blurdoesn't), so one handler on the form hears every field. CSS has the same "after you've touched it" rule as:user-invalid, but a text message needs JS.- Color alone isn't enough: the message says what's wrong. It sits outside the
<label>(inside, it would become part of the field's name), linked witharia-describedby. The border reacts toaria-invalid, so what's seen and what's heard can't disagree. - Browsers compile
patternin the regexvmode, where(,)and-inside[ ]must be escaped. An invalid pattern is silently ignored. - Svelte trims whitespace at the start of an element:
" *"printed as"*". A CSS margin instead.
Links
Next
- Editing the skills for the selected profile: hide one, reword it, add my own. Kept in
localStoragelike my details.