hit counter script
Welcome to MSDN Blogs Sign in | Join | Help
ASP.NET MVC 2 Preview 2: Client-Side Validation

In my opinion one of the most interesting features of ASP.NET MVC 2.0 Preview 2 is the support for client-side validation.

How-to enable Client-Side validation

Client-side validation enable your application to perform client-side validation based on model’s validation by JQuery validation library. Ok, it’s simpler than it sounds.

The client-side validation framework is even extensible, but for now it suits my simple needs.

In my post on ASP.NET MVC 2.0 Preview 1 I’ve added server-side validation to my application model using metadata as ASP.NET Dynamic Data does. So, for example, in the the picture below I’ve added a partial class to my model and a class for metadata describing my requirements for validation. In the following code the attribute ‘Titolo’ must be not null, and I’ve added an Error Message if something’s wrong.

image

If I run the application all works fine: I show a view page to the user, user enters some value and press Save to submit to server. If the user doesn’t fill the title field he gets an error as soon as we made a POST to server. You can see the page flickering in the browser.

Client-side validation prevent your app going to the server for validation and all happens client side, but using the rules you have defined on the partial class you saw before.

To enable client side validation, you can follow these easy steps:

  • use Html.EnableClientValidation method on the View file
  • insert three javascript files : jquery-1.3.2.js, jquery.validate.js, MicrosoftMvcJQueryValidation.js. These files are included in the Scripts directory coming with ASP.NET MVC 2 Preview 2 project template. 

I’ve put the code in my view page for editing some simple fields.

image

Now I can run my application and see the results, I’m using HttpWatch to see that no requests are performed to the server and the validation logic works as expected.

image

As soon as I type something into the textbox the red message disappears.

Nice feature! You can download the ASP.NET MVC 2 Preview 2 from here and read about all the features on Phil Haack post.

Enjoy :-).

Posted: Saturday, October 03, 2009 1:12 PM by pietrobr
Filed under:

Comments

Edward said:

awesome! :)

what font is used for logo "REMIX"?

Thanks a lot

# October 4, 2009 2:26 AM

pietrobr said:

it should be a big big gif :-)

# October 4, 2009 12:00 PM

Seth Rowe said:

Thanks for the post - while obvious once I read it, I was missing the Html.EnableClientValidation call!

# October 7, 2009 9:37 PM

pietrobr said:

I missed it too the first time :-))))

# October 8, 2009 3:46 AM

Richard said:

I've just run across an annoying "feature" of the Client-Side Validation that I'm wondering if someone with better js understanding than myself might be able to solve.

YSlow recommends that javascript be loaded at the end of the page. All good. Add the references to the required .js files at the bottom of the Site.Master and all is good, until you want to use ClientSideValidation...

This injects the EnableClientValidation call immediately after the form being rendered (as outlined in a future post, it renders it as part of the Dispose method on the Form), however for some reason that escapes me this call never gets invoked (because the function definition is not yet loaded?).

Any ideas how I can still load the .js files at the end of the page and also use Client-side validation?

Cheers

Richard

# November 3, 2009 3:14 PM

pietrobr said:

Unfortunately is not always possible, this is one of the case..

# November 5, 2009 12:34 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker