3 ms·
That's exactly the case where the "customValidity" attribute shines! I have nothing against regex and the "pattern" attribute is the way to go for many cases,
by everdimension 2y ago
That's exactly the case where the "customValidity" attribute shines!
I have nothing against regex and the "pattern" attribute is the way to go for many cases, but having this is an alternative is also very nice:
const valid = value.length => 4 && isAlphanumeric(value);
return (
<Input
value={value}
customValidity={valid ? 'at least 4 alpha characters' : ''}
/>
)
- everdimension 2y agoYeah well I promise it does read nicely when there's formatting which HN comments do not allow :)
- kaoD 2y agoAFAIK customValidity is not an attribute and you can only use an imperative setCustomValidity API which is terrible cumbersome to use in a declarative framework like React.
- everdimension 2y agoYeah this is exactly what I'm writing about in the article :)