vUX / FormValidator / v4.1.0

Rules in, messages out.

Write each field's rules as a small object. FormValidator checks them in a fixed order, puts the first failure under or beside the field, and submits the form behind its own loader. Leave a field, or press Create account.

last validate()-
formOk()-
submit()-

01Playground

Write a message straight onto the field, run your own rules through validate(), recolour the boxes, pick a loader and submit. Settings that initialize() reads rebuild the validator; the rest apply to the running one.

message.write()
validate()
Colours (custom properties)
borderfilltext error warning success
Submit config
specimen form

CSRF _token = tok-initial

ready
Actions
Event log
Equivalent code

                

02One form, its own colours

The boxes read --error-color, --error-background-color, --error-border-color and the warning and success pairs. Set them on a form and only that form changes. Both cards below run the same three message.write() calls.

Stylesheet defaults :root

Themed on the form #themeViolet

The themed card's CSS
#themeViolet{
    --error-color:#f0d4ff;   --error-background-color:#2b1840;   --error-border-color:#c8a2ff;
    --warning-color:#b9f3ff; --warning-background-color:#0f2a33; --warning-border-color:#5ccfe6;
    --success-color:#101a06; --success-background-color:#a7e05f; --success-border-color:#c8f08f;
}

03Formatters

FormValidator.format is static: no instance, no initialize().

1,234.5

currencyField(number, sep)

Groups the whole part only, keeps the sign and the decimals, and returns a string.

1.01

roundToDec(value, decimals)

Rounds through exponent notation, so 1.005 becomes 1.01, not 1.

0-9

integerField(input)

Strips every non-digit as it is typed. Paste +1 (555) 010-99 and see.

value = ""
a-b-c

wordSeperator(input, sep, n)

On blur, joins the words with the separator repeated n times. Here "-", twice.

leave the field to join

04Misuse throws

The constructor, every setter and every method check their arguments. Each button runs one bad call on a fresh validator and prints what it threw.

No error yet. Press one of the buttons above.

Two checks are lazy, and the last two buttons show it: validate() checks location only when it writes a message, and a size rule is parsed only when a file is chosen.