Reputation: 3291
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
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
Reputation: 964
The best way is two way validation -
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