Open this example in the interactive library
Live preview
Source code
// components/HexValidationField.tsx
import { useState } from "react";
export default function HexValidationField({ hex = "#bc805a" }: { hex?: string } = {}) {
const [v, set] = useState(hex);
const valid = /^#[0-9a-f]{6}$/i.test(v);
return (
<label>
Six-digit colour
<input value={v} aria-invalid={!valid} onChange={(e) => set(e.target.value)} />
<output>{valid ? "Valid colour" : "Use #RRGGBB"}</output>
</label>
);
}
