Los Techies : Blogs about software and anything tech!

jQuery Validation and ASP.NET MVC Forms


 

We have a standard that is part of our definition of done that includes adding client side validation to any page that contains a form. We always require server side validation, but I really like this standard since I'm all about progressive enhancement. We have adopted Jörn Zaefferer's Validation plug-in (to which I have committed) as the de-facto plugin of choice. Our previous forms, while on MVC Beta, had the following HTML generated:

<input type="text" name="PostalCode" id="PostalCode" />

Since we have upgraded to MVC version 1.0, our HTML is now generated like this:

<input type="text" name="Address.PostalCode" id="Address.PostalCode" />

This introduced a problem, at least temporarily, for the validation plug-in. Whereas the property of the rules and messages was simply "PostalCode" before, it now must be "Address.PostalCode", which causes a syntax error.

rules: {
    Address.PostalCode: { required: true; digits: true; length: 5; }
}

The beauty of this excellent language is that there is probably a way to handle this. There is a super simple solution, which took me one trial and no errors to figure out:

rules: {
    'Address.PostalCode': { required: true; digits: true; length: 5; }
}

This was a good reminder to me that JavaScript isn't bad, the DOM is and that you can do pretty much anything in JavaScript.

Also, you can find this buried in the documentation.

Kick It on DotNetKicks.com
Posted Aug 25 2009, 11:48 PM by Chris Missal
Filed under: ,

Comments

Twitted by ChrisMissal wrote Twitted by ChrisMissal
on 08-26-2009 1:25 AM

Pingback from  Twitted by ChrisMissal

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - August 25-28, 2009
on 08-26-2009 7:15 AM

Daily tech links for .net and related technologies - August 25-28, 2009 Web Development Clean Web.Config

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - August 25-27, 2009
on 08-26-2009 7:16 AM

Daily tech links for .net and related technologies - August 25-27, 2009 Web Development Clean Web.Config

Twitter Trackbacks for jQuery Validation and ASP.NET MVC Forms - Chris Missal - Los Techies : Blogs about software, programming and [lostechies.com] on Topsy.com wrote Twitter Trackbacks for jQuery Validation and ASP.NET MVC Forms - Chris Missal - Los Techies : Blogs about software, programming and [lostechies.com] on Topsy.com
on 08-26-2009 2:14 PM

Pingback from  Twitter Trackbacks for                 jQuery Validation and ASP.NET MVC Forms - Chris Missal - Los Techies : Blogs about software, programming and         [lostechies.com]        on Topsy.com

Balaji wrote re: jQuery Validation and ASP.NET MVC Forms
on 08-26-2009 4:46 PM

Great article!  Thanks for taking the time to explain jQuery Validation and ASP.NET MVC Forms. I've been thinking about similar topics lately, and it's good to see that I'm not alone.  What do you think about JavaScriptMVC – Include?

Chris Missal wrote re: jQuery Validation and ASP.NET MVC Forms
on 08-26-2009 4:57 PM

@Balaji

I actually heard today that there is going to be jQuery validation baked into ASP.NET MVC 2.0!

jQuery Validation and ASP.NET MVC Forms - Chris Missal - Los … wrote jQuery Validation and ASP.NET MVC Forms - Chris Missal - Los &#8230;
on 08-26-2009 10:30 PM

Pingback from  jQuery Validation and ASP.NET MVC Forms - Chris Missal - Los …

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems