DEV Community

Discussion on: Javascript Limit Characters in textbox with HTML & CSS

Collapse
lukeshiru profile image
LUKESHIRU • Edited on

Instead of setting the styles through the style property directly, you could use setCustomValidity and then use the :invalid pseudo-selector. This also blocks the submit of the form which is ideal if is invalid. I made a demo here so you can check it out:

Notice that there are some other differences like some currying, some fixes in the syles so it doesn't break like your demo because is not using margin but position instead, and some use of CSS custom properties.

Cheers!