Uri
Uri

Reputation: 3291

How should form validation work?

I would like to know in general, how is it recommended to validate HTML forms? By submitting the form (with POST) and then showing the validation errors, or by validating it on the same page with JavaScript (or both)? We are using Django and the default validation is by submitting the form, but I saw that some websites validate forms with JavaScript. What is the recommended way to validate forms? Especially for required fields which may be empty or blank.

Upvotes: 0

Views: 52

Answers (2)

T4NK3R
T4NK3R

Reputation: 4363

Both definitely.
First in javascript - to not bother the server with insufficient data.
Then on the server - to defeat hackers (and users with javascript disabled).

In both cases, I prefer to collect all error-messages in a single string,
so the user can correct them all before trying again.

"Inline" / "on-the-fly" checking (visually indicating when a field is OK)
is extra icing on the cake : )

Upvotes: 2

Abhishek Prakash
Abhishek Prakash

Reputation: 964

The best way is two way validation -

  • validate at the client side, but sometimes if the js code is not minified, then it can be hacked by using developer tools
  • So, to be 100 percent safe, use validation at the server side too.

The best way to validate in client side is use HTML5 form validation and for polyfills use some plugins(there are plenty of jquery plugins). And creating one's one script for validation is always a choice when noting works.

Upvotes: 1

Related Questions