views:

206

answers:

9

What is the point in validating your HTML forms using Javascript, if you are always going to need to validate the forms using PHP anyway? I realize that you get a speed boost from this, and its more convenient to the user, but beyond that, is the time spent on it worth it? If anyone has any good evidence on this I would love to hear it.

Thanks for any help!
Metropolis

UPDATE

After receiving numerous answers I would like to change the question a little. We all know that javascript is much more convenient for the user and it gives faster feedback. What I am wondering is: Has anyone ever seen any "evidence" that its worth it? Or do we just do it because it makes things a little better and everyone says we should? The speed difference is not that significant, and as the internet gets faster javascript validation will become even more obsolete I would think.

I am starting to wonder if the time spent validating a page using javascript could be better spent.

A: 

PHP runs serverside, javascript runs clientside. You don't want your server crunching form validation when you can get the clients computer to do so. Plus it saves bandwidth.

Jes
well you always need to validate the form serverside anyway...
darma
Coming up next: client side sql injection prevention.
Mchl
I already know those things....Thats why I said "I realize you get a speed boost from this". What I want to know is, do I get anything else? Because it takes time to validate on both ends when the php is needed, but the javascript really isnt.
Metropolis
Wrong, wrong, wrong, WRONG!
Liam Spencer
...wrong, wrong, wrong, wrong
Matt Briggs
wrong wrong wrong wrong
Tom Gullen
This is not wrong, it's rwogn.rwogn, rwogn, rwogn, rwogn, rwogn, rwogn, rwogn, rwogn
Vinko Vrsalovic
+2  A: 

It's all about usability. It is much more convenient for the user to read what errors they have made before the page reloads, rather than continuously submit and reload the page. It can also give a nicer look with some AJAX and the likes, rather than a reload of the page and the very ugly looking red error messages, I think. So the advantage? Much more usable than having server side validation alone.

Liam Spencer
+14  A: 

Ideally, you validate through javascript and (in your case) PHP.

Both validation methods will work in-tandem to ensure you get the most robust and user friendly functionality possible for your end user.

You will use client-side validation to ensure that all fields are filled in, email addresses are valid, etc.. this will provide instant feedback and won't burden your servers or the user's internet connection.

you validate server-side for security. You can control everything on the server and nothing on the client machine. It's here that you ensure that all entered data is non-malicious and correct.

Keep this in mind: if you are only going to go with one type of validation, choose server-side validation because it is more secure. You should never rely on client-side code for any kind of security.

Using both types of validation gives you the best of both worlds (responsiveness and security) while having none of the downsides. Of course, this means you have to write more code, but in my opinion, it's worth it.

EDIT: In response to the comments

Yes, you have to write more code this way... As a rule of thumb, if it's harder for the programmer, it's easier on the user. It might not make sense in some budgets to do both types of validation and that's a call you're going to have to make. Just make sure your server side validation is rock-solid regardless.

Yes, time is money, and time invested in improving the user's experience is time well spent. If you can't afford to do it now (deadlines/schedule/budget) then do it when you can.

Robert Greiner
So the only thing you really get is speed? But all the extra time you spend doing that javascript validation to save a few seconds (or less in most cases)? Dont get me wrong, I have always done both up until now. But I just came up to a form with 14 fields and Im starting to think its not worth the hassel.
Metropolis
Stop thinking about what is convenient for YOU, and start thinking about what is convenient for the USER. ;)
Liam Spencer
@Liam LOL I agree......But time is money.
Metropolis
@Liam Spencer, pfft if the user wants javascript they can bring their own ;)
@Metropolis see comments.
Robert Greiner
@Metropolis if validating 14 fields isn't worth the hassle, you should consider looking into (alternative) JavaScript libraries to handle the gruntwork for you.
Hooray Im Helping
If you regularly do form validation and have not invested the time in learning a library that does it for you (and there are plenty that do), or rolling out your own, reusable, solution, I would be inclined to say "you're doing it wrong." Hell, I never do any form validation and I still decided to make my own validation library as a learning exercise.
CD Sanchez
How do you know the time "invested" is worth it? It does not improve the users experience that much. You say its "well spent" without actually knowing that it is. Everyone just goes around saying it is, so we assume it is.
Metropolis
A: 

To provide a better user experience.

The feedback on JS validation is faster, and therefore better than server-side validation on form submit.

daotoad
A: 

The main point of JavaScript validation (when available) is that it improves the user experience. A round-trip to the server requires a page load and the associated annoying flicker as it redraws. Validating in JavaScript code allows you to display a message without all that.

That being said, server-side validation is still required since JavaScript isn't always available (NoScript is quite popular) and because a malicious user will bypass the JavaScript.

ThatBlairGuy
A: 

Particularly for database backed websites, it tends to be that you need to do server side validation anyway. e.g. to make sure you're inputting valid data into a database or other system. Depending on what the website is updating this could be absolutely critical.

However, client side validation can provide a better user experience. It can be used to provide instant feedback. e.g. when you move focus away from a text box a validator can provide instant feedback which is great when you're filling in a long complicated form.

The bottom line is that you will still need to input good data into your database. And the more correct the information stored in there, the less problems with the system you'll have later. You need both.

e.g. What if someone updates the website code in the future and breaks the validation? or someone writes a script to automate inputting data, bypassing your web front end all it's validation?

I'll say it again. You need both.

Matt H
A: 

...i think you're also keeping your karma cleaner, when hundreds or thousands of your users don't wish you burn in hell for making them fill in 5-7 fields (with textarea) to be informed on the next page they mistyped their email so they have to start all over again :D it doesn't eat up much of my time to incorporate javascript, id say 1-2 minutes maximum for 1 form. and it saves lots of nerve cells of my users. be a humanist! love ur neighbour!))

SEO
A: 

Client-side validation allows for an increased user experience. The feedback you give to the user leads to less frustration, less errors, more conversion, more money. You generally have a better response rate with this kind of validation, which is very valuable.

An high quality software needs this. Users feels happy, and they will spread their joy. A user who has a bad experience won't come came and won't tell his friend.

It's not only decoration when you get to business and sales. ;) The return on investment is worth it.

Savageman
A: 

Easy.

Javascript to help the user enter correctly formatted data. PHP to make sure whatever enters your script gets cleansed before further processing.

Ofcourse you'll have to do both. Users want it, your customers want it and frankly, you think it's fugly getting php errormessages after submit aswell.

I don't think the argument of having to code extra .js which presumably would eat up your time/budget holds any thruth. There's so many libs and scripts outthere, either one will enable you setting up disco validation in no time at all. However, don't get carried away with dealing out eye candy. .js validation is just there to help. Not to impress.

Amelia