hform/guides

HTML Form Validation Without JavaScript

Most form-validation tutorials reach for a JavaScript library in the first paragraph. For a normal business form that's backwards: the browser validates required fields, email formats, lengths and patterns natively, in every language your visitors speak, with zero code to maintain. Start there; add JavaScript only for what HTML can't say.

What the browser does for free

Put the rules in the markup and the browser enforces them on submit — it blocks the send, focuses the first bad field, and explains the problem in the visitor's own language:

<label for="email">Email</label>
<input id="email" name="email" type="email" required>

<label for="message">Message</label>
<textarea id="message" name="message" required
          minlength="10" maxlength="2000"></textarea>

That's a complete validation setup for a contact form. No library, nothing to update, and it works with JavaScript disabled.

The attributes worth knowing

Small attributes, outsized comfort

Two more that aren't validation but belong in the same pass: autocomplete (e.g. autocomplete="name", "email", "tel") lets the browser fill the form from what it already knows — the fastest form is one nobody types. And inputmode="numeric" brings up a number pad for digit-only fields that aren't really numbers, like a postcode.

Don’t over-validate

Every rule you add rejects some real person. Names don't match letter-only patterns (O'Brien, José, 李). Postcodes come in more formats than any regex you'll write. Phone numbers with country codes, spaces and dashes are all valid. The test for any pattern: can you name an honest value it would reject? If yes, loosen it — a business form's job is receiving messages, not grading input. Validate strictly only what's truly structural, and let humans be humans everywhere else.

Where JavaScript earns its place — and what it never replaces

JavaScript is for presentation: showing errors inline as the visitor types, styling messages to match your brand, submitting without a page reload. Fine — added on top of the HTML rules, they keep working without it.

What client-side validation never does is protect your endpoint. Bots don't read your attributes; they POST straight to the address. Whatever receives your form must enforce its own limits — hform's endpoint caps field counts and sizes, rate-limits, and filters spam server-side regardless of what the HTML said. If you run your own endpoint, do the same.

Quick answers

Is HTML validation enough on its own?

For the visitor’s experience, usually yes. For safety, no — anything can POST directly to your endpoint, skipping your HTML entirely. The receiving end must apply its own limits; browser validation is comfort, not defense.

Can I customize the error messages?

The browser’s built-in messages come in the visitor’s language for free, which is hard to beat. Truly custom text needs a few lines of JavaScript (setCustomValidity), and it’s presentation only — keep the HTML rules as the source of truth.

Why does my pattern reject valid input?

Because real-world values are messier than regexes assume — names with apostrophes and accents, postcodes with spaces, phone numbers with country codes. Reserve pattern for genuinely structured values, and when in doubt, drop the rule.

Free — no signup

These defaults, built in

The hform builder writes this kind of HTML for you — real labels, the right input types, a honeypot, validation that respects your visitors. Build a form in two minutes; free plan included, plain pricing beyond it.

More form craft: Phone fields · File uploads · Form length — or see all guides.