Skip to content

Comment on Today, Web Development Sucks

Comments

You can still handle your validations server-side in a single-page application. The client JavaScript code sends a JSON packet to the server containing whatever data it needs to process. The server responds with a JSON packet which looks like

    {"status": "ok", "data": ...}
to signify success, or
    {"status": "validation_failed", "details": {"email": "malformed email"}}
to signify validation failure. Then the client-side code updates the UI appropriately. The client JS code does not bother with validation at all. If this seems wasteful in terms of hitting the server, remember that you have to talk to the server for this task anyway.

A user makes a mistake in typing email, adds in rest of form. Presses submit. Form takes 3s to send. Throws validation error and highlights email. User corrects it and sends it again (hopefully you're not punishing your users by clearing the form).

Or, you could present client-side validation which alerts user on focus out.

Other uses for client-side validation:

+ Multiple emails (Gonna verify them all in many calls?)

+ Prerequisite for further steps

Of course, have server-side validation. Client-side validation is convenience for the user.

I use this very method for dealing with validation (though mine returns a HTTP 400 on validation error). Trivial stuff, like a field being required or not, I check on the client; everything else is checked on the server. I've written a small library to plug this into jquery. It's called quaid, and the validation module docs/examples are: http://benogle.com/quaid/validation

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.