7 ms·
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
by gcv 16y ago
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.
- Joakal 16y agoA 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.
- mrbogle 16y agoI 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 http://benogle.com/quaid/validation