Forms & Validation

Forms built for dense B2B interfaces: one class for every field, validation without a single line of JavaScript, and a forced 44px height. Ready-made layouts are in the examples.

1. Three principles

molique forms differ from typical libraries in three places. Once you understand these three things, the rest of the page is just a list of classes.

Principle What it means in practice
One .input class <input>, <select> and <textarea> all get the same class. There's no separate .form-select or .form-textarea to remember.
Validation without JS Error messages are shown by CSS alone, via the :user-invalid pseudo-class. You don't wire up any script - see section 4.
44px minimum Every field and button has min-height: var(--target-size-min) - the WCAG 2.2 AAA touch-target requirement. Don't set it lower.

Focus the second field, type anything, and leave it - the message will appear with no JavaScript.

2. The field and its variants

<input type="text" class="input">
<select class="input">…</select>
<textarea class="input"></textarea>

<input type="text" class="input input-sm">   <!-- shorter -->
<input type="text" class="input input-lg">   <!-- taller -->
Class On What it does
.input input, select, textarea Border, padding, rounded corners, focus ring and a minimum height of 44px.
.input-sm .input A shorter variant for dense tables and filter bars.
.input-lg .input Taller variant - contact forms, hero sections.
.input-range input[type="range"] A slider with a consistent look across browsers.
.input-color input[type="color"] A color picker as a neat square instead of the native button.
.textarea-expandable <textarea> A single-line-height field that expands on click. Requires placeholder=" " - see pitfall #1.

Number field with no spinner

input[type="number"].input has no native spinner - in a dense interface those arrows are too small to hit. Use the .qty-input component with + / − buttons instead, which also supports the keyboard and scroll.

3. Floating labels

The label sits inside the field and floats upward once the field isn't empty. It saves vertical space - in a twelve-field form that's the difference of a whole screen's worth of scrolling.

The mechanism rests on the question "is the placeholder visible?". The :placeholder-shown pseudo-class is true exactly when the field is empty - CSS has no other way to check that. Hence the two unusual requirements below.

<div class="form-floating">
  <!-- placeholder=" " with a space - without it the label won't return to place -->
  <input type="text" class="input" id="name" placeholder=" ">
  <!-- label MUST come AFTER the input: CSS links them with a sibling selector -->
  <label for="name">Full name</label>
</div>
Requirement Why
Field inside a .form-floating container Gives the label a reference point to position itself against.
placeholder=" " (with a space) An empty placeholder="" doesn't count as shown, so the label would stay up top permanently.
<label> after the input The rule is .input:not(:placeholder-shown) ~ label. A sibling selector only reaches forward - a label placed above the field will never be matched.

4. Validation without JavaScript

All validation rests on :user-invalid. The difference from the older :invalid is fundamental: :invalid is true from the moment the page loads, so an empty required form lights up red before the user does anything. :user-invalid only kicks in once the user has entered the field, typed something wrong, and left it.

<div class="form-floating">
  <input type="email" class="input" id="email" placeholder=" " required>
  <label for="email">Work email</label>

  <!-- Sibling of the input. CSS shows it on its own, when needed -->
  <div class="feedback-invalid">Invalid email address.</div>
</div>
Element Role
required, type, pattern Plain HTML attributes. They're what defines what counts as an error - you don't write CSS rules for this.
.feedback-invalid The error message. Hidden by default; shown by the rule .input:user-invalid ~ .feedback-invalid, so it must be a sibling of the field, placed after it.

What about messages from the server

:user-invalid only handles rules the browser knows about. Backend errors (username taken, invalid discount code) you still render yourself - just print .feedback-invalid and give it visibility, since the field isn't formally invalid.

5. Other controls

Checkboxes and radios

Native controls look different in every browser, so molique turns off their appearance with appearance: none and draws its own. The checkmark is an SVG icon inlined into CSS as a data URI - no extra files, no icon fonts. Keyboard behavior stays native, because the underlying element is still a plain <input>.

<div class="form-check">
  <input class="form-check-input" type="checkbox" id="consent">
  <label class="form-check-label" for="consent">I accept the terms</label>
</div>

Field groups

Glues a field together with text, an icon, or a button into one smooth rectangle - the rounded corners between them vanish automatically, regardless of element order.

<div class="input-group">
  <span class="input-group-text">https://</span>
  <input type="text" class="input" placeholder="site-address">
  <button class="btn btn-primary">Check</button>
</div>

File selection

The native <input type="file"> can't be styled and looks different in every browser. The component overlays it with a drop zone - the field itself stays underneath, so handling, file dragging, and accessibility all remain native.

<div class="file-upload">
  <input type="file" name="document">
  <div class="file-upload-icon"><svg class="icon" aria-hidden="true"><use href="img/icons-sprite.svg#ph-file-text"></use></svg></div>
  <p class="file-upload-name">Drag a file here or click</p>
</div>

The filename replaces itself

The auto-loaded js/modules/molique-file-upload.js module listens for the change event on the hidden field - once a file is picked (by click or by drag) it overwrites the content of .file-upload-name with the file's name. You don't have to add the element by hand: if it's missing from the markup, the module will insert it itself after the first file is picked.

Class Component What it does
.form-check Checkbox / radio Wrapper: puts the control and description on one line.
.form-check-input Checkbox / radio The control itself. Same style for checkbox and radio - the shape follows the type.
.form-check-label Checkbox / radio The description. Clickable, so it enlarges the touch target.
.form-floating Floating label Field container with the label inside (section 3).
.feedback-invalid Validation Error message (section 4).
.input-group Field group Glues a field together with add-ons into one rectangle.
.input-group-text Field group A non-clickable add-on: unit, prefix, icon.
.file-upload File selection Drop zone wrapped around the native field.
.file-upload-animated File selection Variant with an animated border that reacts to hovering.
.file-upload-icon File selection Icon inside the zone.
.file-upload-name File selection Caption that JS overwrites with the chosen filename. Optional in the markup - without it, the module adds it itself.

6. Masking and the keyboard

One data-mask attribute formats the field as it is typed and sets the things that are easiest to forget: inputmode (the numeric keypad on a phone), autocomplete, pattern and maxlength. Without JavaScript the field stays an ordinary .input - it loses the automatic separators, not the validation.

The fields below are real - type into them, paste a number with spaces, try editing in the middle.

Nine digits, formatted 123 456 789
<!-- a preset: the module fills in the rest of the attributes -->
<input type="tel" class="input" data-mask="phone-pl">

<!-- your own mask: # digit, A letter, * digit or letter -->
<input type="text" class="input" data-mask="### ##-##">

<!-- the clean value (no separators) in an extra hidden field -->
<input type="text" class="input" data-mask="iban" data-mask-unmasked-name="iban_raw">

Shipped formats

Preset Format Notes
phone-pl 123 456 789 Pasting a number with a +48 or 0048 prefix drops the prefix, not the end of the number.
postal-pl 00-950 -
postal-de 10115 No separator - it contributes a digits-only filter and a length limit, nothing more.
nip-pl 123-456-78-90 Polish tax identification number.
card 4111 1111 1111 1111 The mask changes itself for American Express (cards starting 34 and 37): 15 digits as 4-6-5.
card-expiry 05/27 -
iban PL61 1090 1014 ... Accepts letters and upper-cases them. Up to 34 characters, groups of four.
nrb-pl 12 1234 5678 ... Polish bank account number: 26 digits as 2 + six groups of four.

Attributes and API

The methods below live on the global MoliqueMask object.

Item Role
data-mask A preset name or a mask of your own. Resolution: a preset by name first, then a literal mask. A misspelled name (phone-PL) warns in the console and the field is left unmasked - rather than silently swallowing every keystroke.
data-mask-unmasked-name Optional. Adds a hidden field under that name holding the value without separators - the visible field still submits the masked version.
format() A pure function, no field involved: format(value, mask). For formatting values arriving from a server.
value() A field value without separators: value(field).
register() A reusable preset of your own: register(name, def), where def is { mask, inputmode, autocomplete, pattern, uppercase }.
apply() For fields added after the page started. Formatting works without it (delegation), but inputmode has to be in place before the user focuses the field.

Not on type="number" or "email"

Those two types cannot carry a caret: reading its position returns null and setting it throws. A mask has nothing to move there, so the module refuses loudly with a console warning instead of quietly dropping the caret to the start. An amount or a card number is type="text" with inputmode="numeric".

Do not write maxlength yourself

The masked value is longer than the bare one, so a single number cannot serve both states. The module derives the limit from the mask. Your own maxlength, if shorter than the mask, truncates the value - you get a console warning, because the symptom (a value one character short) reads as a backend problem.

What reaches the server

By default what the user sees - the value with its separators. A plain <form> works with no extra code and nothing is swapped behind the user's back. For the clean value use data-mask-unmasked-name, or normalise on the server.

The pattern accepts both spellings

Every preset pattern accepts the value with and without separators. If it demanded the spaces, a user without JavaScript typing bare digits would get a false validation error - worse than having no mask at all.

7. Variables

Variable Default Controls
--input-border-width 1px Field border thickness.
--input-padding-y calc(var(--spacing-unit) * 1.25) Vertical padding. The -sm and -lg variants override it themselves.
--textarea-rows-expanded 6 How many rows .textarea-expandable expands to.
--target-size-min 44px Minimum height of fields and buttons.
--focus-ring-color primary, 25% opacity Focus ring for a keyboard-focused field.

Full list: CSS Variables.

8. Pitfalls

1 Missing placeholder=" "

The most common mistake on this page, and it affects two components: floating labels and the expandable textarea. Both ask CSS :placeholder-shown, i.e. "is the field empty?". Without a placeholder the answer is always "no" - the label stays up top, and the textarea never collapses.

<!-- WRONG - an empty placeholder doesn't count as shown -->
<input class="input" placeholder="">
<input class="input">

<!-- RIGHT - a space inside -->
<input class="input" placeholder=" ">
2 Label above the field instead of below

Instinct suggests writing <label> before <input> - that's what a regular form looks like. With floating labels that won't work: the rule uses the sibling selector ~, which only reaches forward. Visually the label will still land in the right place, since CSS positions it.

The same applies to .feedback-invalid - a message placed before the field will never show up.

3 Using :invalid instead of :user-invalid

:invalid is true immediately after the page loads, so a registration form greets the user with a full set of red fields before they've clicked anything. It looks like a broken page. :user-invalid waits for interaction - and that's what molique uses.

4 Shrinking field height

Tempting in a dense CRM: height: 32px on .input and more rows fit on screen. This breaks the 44px touch target, making the field hard to hit with a finger on a phone. If you really need it tighter, use .input-sm instead - it reduces padding but keeps the lower bound intact.

9. Related pages