Is JavaScript validation bad?

Javascript

Javascript Problem Overview


It has been long time since we have been validating our forms using JavaScript. I am sure this must be the case with most other developers.

Question:

What if the user (or probably a bad guy) disables JavaScript?

You are lost!

  • Is JavaScript validation worth of it?
  • Should we ever use it now?
  • Are there any solutions to this?

Correct me if I am wrong.

Javascript Solutions


Solution 1 - Javascript

> Is JavaScript validation worth of it?

Yes, as it provides a better user experience and preserves bandwidth.

> Should we ever use it now?

Yes, for the aforementioned reasons.

> Are there any solutions to this?

Yes, use server-side validation as well.

Solution 2 - Javascript

> What if the user (or probably a bad guy) disables javascript?

As said before: Simply do not rely on the client. Never do so. Check everything on the server again.

> Should we ever use it now?

Yes - so the user immediately sees what's wrong. Otherwise he had to post back the data first which may take a while. By the way you reduce traffic to your server.

It's simply more inuitive.

//EDIT: BTW: The ASP.NET ValidationRules contain both client-side and server validation as far as I know.

Solution 3 - Javascript

Javascript validation is good because it provides a better user experience.

You should however never rely on it and should validate on the server regardless.

Solution 4 - Javascript

If you're looking to save time, go with server-side only. If you want better performance and user experience, add client-side validation afterward. Never rely on client-side validation, for the reasons you state. All critical validation should occur on the server ... even if duplicated on the client.

Solution 5 - Javascript

JavaScript improves user interaction for your product or service. Users interaction (user input and machine response or vice versa) is a vital characteristic of our applications. As we all experienced, products are getting more interactive ever than before. And this interaction part can (only) be crafted in JavaScript (ActionScript for Flash Player). We would all agree with this - there is always a calculated amount of work that can be transited to the client side (machine) to avoid calls without bothering them to send to the server(s). There are many many applications which are heavily dependent on client-script scripting. And if they found you do not allow required scripting they asked for it leaving a message in noscript tag. But I think everyone wants to have it enabled as we all fire up a tab with Gmail, Facebook, etc.

However, this still should not be ignored as we are keen to grap every single opportunity (audience/customer) and work with is at least better than falling apart. It still works!

As a Microsoft Development Platform user, there is a convenient solution on .NET platform. That don't require dual effort on such issues. Make use of your client-side validation while scripting is disabled by using Page.Validate() and Page.IsValid.

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack) {
        Page.Validate(); // If you missed, then you got the second chance ...
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (Page.IsValid) { // Confirm you do a proper validation before moving to perform any process
        Response.Write("Done!");
    }
}

I hope this will help.

Solution 6 - Javascript

Using JavaScript is not wrong. We've been using it since a long time. It is used for applying client-side validations.

Still, we should implement server-side validation so that a bad guy would not be able to break the application.

Solution 7 - Javascript

Client-side (Javascript) validation is about usability, nothing else. If the cost of implementing is not worth the perceived increase in usability, then don't spend the time on it. These days it's pretty easy to do though!

I don't think you can do without server-side validation, however, since this is the only thing that provides you with any security.

Solution 8 - Javascript

If you learn only one thing from this topic, let it be this:

Never — under any circumstances — trust data from the browser and always validate request data on the server-side.

> Should we ever use it now?

Yes, definitely. You do not need to validate an empty field on the server side. It is not something like validating an email's availability (uniqueness of email). If you are going to reject that empty field anyway, there is no point of sending it to server and making server do extra work for it.

Solution 9 - Javascript

You have to validate it on sever-side, javascript is good to validate form, but people can disable javascript, or use another javascript to hack it, so validation on server-side is a must.

Solution 10 - Javascript

JavaScript is useful for client side validation. But you cannot rely only on them. You must use server-side validation against the posted data. JavaScript just prevents unnecessary posts to the server.

Solution 11 - Javascript

You can make server and client-side validation pretty painless by using a framework that supports both. In the past, for ASP.NET I used the Peter Blum validators:

http://peterblum.com/

With this, you drop the validation controls onto your page, hook them up to the inputs (textboxes, drop down lists etc), and specify the validation properties (minimum length, required, error message etc). When the page runs, the framework spits out equivalent code for both the client (JavaScript) and server (ASP.NET) to perform your validation.

Without such a framework, as other posters have pointed out, validation can be laborious.

I'd be interested to know of anything similar for PHP or other technologies.

Solution 12 - Javascript

You should have multiple layers of validation.

Validation on the client Side

This is definitely useful because validation can be done without having to go to the server. Requests get to the server once they are validated - saves some traffic.

Validation on the server side

If javascript is disabled then the server should also incorporate a level of protection - validation in order to disallow erroneous requests.

Solution 13 - Javascript

In a multi-tiered / service orientated environment validation should exist on multiple levels to allow for better reuse while still maintaining a secure application. Validation on the client side, whether in a desktop app, or web site/application should be there for a better user experience to prevent postbacks to the server everytime for validation, hence costing more bandwidth and user time. If client-side validation cannot be moved entirely to the front end then consider using ajax for a partial postback to a server side validation routine, while retaining a better customer experience but allowing a programmer to maintain the validation rules centrally.

Second to the client side, but more importantly, server side code should validate the data before persisting it via a data layer or passing it to another server side method/service, to employ business rules around the data and help prevent errors in data integrity. Lastly, the persistence layer itself (the immediate interface to the database or other storage mechanism) should validate the data being stored, again to prevent errors in data integrity and possibly further business rules. The last thing you want is a data store with useless data.

Employing this method will keep you data secure and integrity in line. On reuse of either you persistence layer, your data layer or your front-end presentation thereafter, in your own site (or via a web service, desktop application or mobile app), if designed properly, these validation routines are already in place and can be re-employed. This should prove to be of great benefit to you alone, and your colleagues and your management, if you happen work in a team.

Solution 14 - Javascript

Is JavaScript validation worth of it?

well,yes it is .Buy using JavaScript validation you can easily take any kind information about client site more over JavaScript validation provides a better user experience

   Should we ever use it now?

Yes you can because of user can see there error or what's they do wrong on real-time

Are there any solutions to this?

yes you can also use server-side validation.But sometime its take more time .it's also insecure

Solution 15 - Javascript

Javascript is a client-side scripting language. Therefore we can use client-side validations by using it. It helps to reduce the load that goes to the server-side. That's why js validation is worthy. And that is the reason for use it for client-side validations. But we should not only depend on js validations. Because the client can turn off the js engine at any time. If so, it causes a big problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSarfrazView Question on Stackoverflow
Solution 1 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 2 - JavascriptMatthiasView Answer on Stackoverflow
Solution 3 - JavascriptcletusView Answer on Stackoverflow
Solution 4 - JavascriptBeep beepView Answer on Stackoverflow
Solution 5 - JavascriptRamiz UddinView Answer on Stackoverflow
Solution 6 - JavascriptRitesView Answer on Stackoverflow
Solution 7 - JavascriptRob Fonseca-EnsorView Answer on Stackoverflow
Solution 8 - JavascriptgurkanView Answer on Stackoverflow
Solution 9 - JavascriptSylerView Answer on Stackoverflow
Solution 10 - JavascriptOzan BAYRAMView Answer on Stackoverflow
Solution 11 - JavascriptMike ChamberlainView Answer on Stackoverflow
Solution 12 - JavascriptoneirosView Answer on Stackoverflow
Solution 13 - JavascriptnealkernohanView Answer on Stackoverflow
Solution 14 - JavascriptFerrakkem BhuiyanView Answer on Stackoverflow
Solution 15 - JavascriptChandima SamarakoonView Answer on Stackoverflow