Elijah Manor
Elijah Manor

Reputation: 18023

ASP.NET MVC Client Side Validation

I am all about using ASP.NET MVC, but one of the areas that I hope gets improved on is Client-Side Validation.

I know the most recent version (Preview 5) has a lot of new features for Validation, but they all seem to be after the page has been posted.

I have seen an interesting article by Steve Sanderson... using Live Validation, Castle.Components.Validator.dll, and a Validator framework he made.

I have used it in my project, but I am hoping something like it will get integrated into the official ASP.NET MVC release. I definitely think the business rules should reside either on the model or in the controller rather than in the View.

Have any of you used a similar approach?

Are you aware of something like this being added to the official ASP.NET MVC release?

Upvotes: 5

Views: 9704

Answers (5)

Adrian Grigore
Adrian Grigore

Reputation: 33318

Have a look at this blog article. It describes how to automatically generate client-side validation rules with xVal and also how to automatically implement remote client-side validation.

Upvotes: 0

Richard Ev
Richard Ev

Reputation: 54137

It looks like this area will see lots of improvement in ASP.NET MVC 2

http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx

Upvotes: 0

Tim Scott
Tim Scott

Reputation: 15205

I agree with other posters, client side validation is strictly for improving user experience.

Check out the JQuery Validation plugin. It's super easy to get started with basic validation -- literally one line of JS plus adding class names to the input controls. It's also very powerful. You can extend to do whatever you want.

Upvotes: 1

Brian Vallelunga
Brian Vallelunga

Reputation: 10201

"Obviously you'll still need to validate your input on the server side for the small percentage of users who disable javascript."

Just an update to this comment. Server-side validation has nothing to do with users that run with JavaScript disabled. Instead, it is needed for security reasons, and to do complex validation that can't be done on the client. A form should always have server-side validation. Client-side validation is only there as a convenience.

A malicious user could easily post data to your form bypassing any client-side validation that you have in place. Never trust input data!

Upvotes: 18

Ben Scheirman
Ben Scheirman

Reputation: 41001

LiveValidation is another helpful javascript library that can help out. See an example (with ASP.NET MVC) here:

http://blog.codeville.net/2008/09/08/thoughts-on-validation-in-aspnet-mvc-applications/

Upvotes: 0

Related Questions