Validate and label a form
Validate meaningful values at submission
Validation checks whether input meets the rules of the task. Native constraints such as required and min can help, but business rules may need JavaScript. Put submission handling on the form so keyboard and button submission follow the same path. In these local demos, no data is sent to a server.
import { useState } from 'react';function titleError(text) { const title = text.trim(); if (!title) return 'Enter a task title.'; if (title.length > 60) return 'Use at most 60 characters.'; return '';}export default function App() { const [title, setTitle] = useState(''); const [error, setError] = useState(''); const [saved, setSaved] = useState(''); function submit(e) { e.preventDefault(); const message = titleError(title); setError(message); if (!message) { setSaved(title.trim()); setTitle(''); } } return <main><h1>New task</h1><form onSubmit={submit} noValidate> <label htmlFor="task-title">Task title (required)</label> <input id="task-title" name="title" required value={title} onChange={e => { setTitle(e.target.value); setError(''); }} aria-invalid={Boolean(error)} aria-describedby="title-help" /> <p id="title-help">{error || 'Maximum 60 characters.'}</p> <p role="status">{error || (saved ? `Saved: ${saved}` : '')}</p> <button type="submit">Save task</button> </form></main>;}An empty or whitespace-only submission is rejected. A valid title is trimmed and saved, then the field clears. The visible hint is linked to the field, and the status region can announce a result; actual assistive technology behavior needs testing. noValidate disables automatic browser submission blocking here so the custom validator can provide a consistent message. The length rule counts JavaScript UTF-16 code units, not necessarily visible characters. This example deliberately uses one field; larger forms may need focus management and an error summary.
Keep numeric editing separate from numeric conversion
import { useState } from 'react';function parseMinutes(text) { if (!/^[0-9]+$/.test(text)) return null; const minutes = Number(text); return Number.isInteger(minutes) && minutes >= 1 && minutes <= 240 ? minutes : null;}export default function App() { const [text, setText] = useState(''); const [result, setResult] = useState(''); function submit(e) { e.preventDefault(); const minutes = parseMinutes(text); setResult(minutes === null ? 'Enter a whole number from 1 to 240.' : `Estimate: ${minutes} minutes`); } return <main><h1>Time estimate</h1><form onSubmit={submit}> <label>Minutes <input name="minutes" inputMode="numeric" value={text} onChange={e => { setText(e.target.value); setResult(''); }} /></label> <button type="submit">Check estimate</button> </form><p role="status">{result}</p></main>;}An empty field, 0, 241, 1.5 or letters are rejected; 25 produces Estimate: 25 minutes. inputMode is a keyboard hint, not a validator. Keeping the input as a string lets users erase it naturally. Converting an empty string with Number would produce zero, which is not an entered estimate. The example accepts ASCII digits only, including leading zeroes, and rejects spaces. Numeric inputs can also use type="number" with min/max/step and valueAsNumber, which can be NaN for an empty or invalid value.
Read-only is not disabled
import { useState } from 'react';export default function App() { const [result, setResult] = useState(''); function submit(e) { e.preventDefault(); const data = new FormData(e.currentTarget); setResult(`Code: ${data.get('code')}; locked included: ${data.has('locked')}`); } return <main><h1>Task details</h1><form onSubmit={submit}> <label>Code <input name="code" value="DEMO-1" readOnly /></label> <label>Locked <input name="locked" defaultValue="Archive" disabled /></label> <button type="submit">Inspect fields</button> </form><p role="status">{result}</p></main>;}The read-only text field cannot be edited but remains focusable and is included in FormData. The disabled field is generally skipped in keyboard navigation and omitted from form submission. Read-only is not a universal alternative for controls such as selects or checkboxes. Neither flag is a security control: a real server must validate data and enforce permissions regardless of browser restrictions.
Practice
Extend the estimate validator with an explicit upper bound of 90. Test blank, 1, 90, 91 and a decimal. Explain why the server must check the same business rule.