Scott Guthrie announced the launch of the Microsoft Ajax CDN on his blog last night. If you have not read his post, I recommend that you read it now to get a general overview of the CDN and how you can take advantage of the CDN to improve the performance of your ASP.NET Web Forms and ASP.NET MVC applications:

http://weblogs.asp.net/scottgu/archive/2009/09/15/announcing-the-microsoft-ajax-cdn.aspx

In his announcement, Scott describes how both the ASP.NET Ajax and the jQuery libraries are included in the CDN. There is one more set of JavaScript files that we added to the CDN today that Scott did not announce: the jQuery Validation library.

If you are not familiar with the jQuery Validation library then you should know that this is one of the most popular form validation libraries used by jQuery developers. We are shipping the jQuery validation library with both ASP.NET Web Forms and ASP.NET MVC when we ship Visual Studio 2010.

Furthermore, we are integrating the jQuery Validation library with ASP.NET MVC. When you add an error to ModelState – for example, by using ModelState.AddModelError() – the error will float up to the client automatically. The jQuery Validation library is the JavaScript library used by ASP.NET MVC to implement client-side validation.

I had the pleasure of meeting Jörn Zaefferer at the jQuery conference in Cambridge last weekend (You pronounce Jörn’s name like Yearn). He has written a really great validation library and now you can use his validation library directly from the Microsoft Ajax CDN.

Here are the URLs for the jQuery Validation library:

The first URL contains the unminified (human readable) version of the jQuery validation library. The second URL contains a minified version of the library that you should use in production applications. Finally, the third URL points to a JavaScript file that contains Intellisense comments for the jQuery validation library that is used by Visual Studio 2010 automatically.

Here’s a simple Web Forms page that uses the jQuery Validation library to validate a Registration form (see Figure 1): 

Listing 1 – Registration Form

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestjQueryValidation.aspx.cs" Inherits="WebApplication2.TestjQueryValidation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Registration Form</title>

    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" 
        type="text/javascript"></script>
    <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.min.js" 
        type="text/javascript"></script>
    
    <style type="text/css">
    
    input.error
    {
        background-color: #FFFF99;
    }
    
    </style>
    
    <script type="text/javascript">
      $(documentReady);
      
      function documentReady() 
      {
        $("#form1").validate();
      };
      
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <h1>Register</h1>
    
    <asp:Label 
        ID="lblName" 
        AssociatedControlID="txtName" 
        Text="Name:"
        runat="server" />
    <asp:TextBox 
        ID="txtName" 
        CssClass="required" 
        runat="server" />

    <br /><br />
    
    <asp:Label 
        ID="lblEmail" 
        AssociatedControlID="txtEmail" 
        Text="Email:"
        runat="server" />
    <asp:TextBox 
        ID="txtEmail" 
        CssClass="required email"
        runat="server" />
    
    <br /><br />
    
    <asp:Button 
        ID="btnSubmit" 
        Text="Register" 
        runat="server" />
    
    </div>
    </form>
</body>
</html>

Figure 1 – Create Movie form

image

 

The Registration form in Listing 1 contains two TextBox controls for a user name and email address. The jQuery Validation library is used to make both fields required and validate that a proper email address has been entered.  There are two things that you need to do to get the jQuery Validation library to work:

  1. Call the validate() method on your form. Calling the validate() method sets up the client validation.
  2. Add the right classes to the fields that you want to validate. In the case of Listing 1, I added a required class to both the txtName and txtEmail TextBox controls and an email class to the txtEmail TextBox control.

The jQuery Validation library supports a number of specialized validation classes including classes like url and creditcard: If you want to read the full documentation for the jQuery Validation library then you can visit the following website:

http://docs.jquery.com/Plugins/Validation

If you liked this blog post then please Subscribe to this blog.
posted on Wednesday, September 16, 2009 1:10 PM | Filed Under [ AJAX ASP.NET ASP.NET MVC CDN ]

Comments

Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Ralph Whitbeck
on 9/16/2009 2:49 PM

This really is excellent news. It was a pleasure meeting you at jQuery Conference. I am glad you were able to meet Jörn and the rest of the jQuery UI team.
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Shahriar Hyder
on 9/16/2009 9:22 PM

Hi Stephen,

Thanks for the nice posting. Until now we have been dependent upon google alone to serve jQuery files but after reading yours, we know that Microsoft is adding popular jQuery plugins to the CDN as well. By the way, I have also tweeted about yours here:

RT @velvetflair: Stephen Walther's Blog: Microsoft Ajax CDN and the jQuery Validation Library http://3.ly/SjZuon
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Thanigainathan
on 9/16/2009 11:28 PM

Hi There,

Very nice feature !

We can now easily integrate these scripts in our application.

Thanks,
Thani
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Praveen Prasad
on 9/17/2009 1:56 AM

Hello Stephen, hope you are doing good.
well i am wondering is it possible like we can integrate server side and client side (as mentioned by you here) validation.
Like if a user has disabled his JS (or some curious user doing some mischievous activity) and bypasses jquery validation on browser. Now validating this page on server and sending back to user with error notification we have write some other logic too.
Is it possible we can integrate both logics.
Like we usually do with asp.net forms (validation controls)
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Stephen.Walther
on 9/17/2009 5:46 AM

@Praveen,

That's a great question! If you are using ASP.NET MVC, you do all of the validation on the server-side. The validation "bubbles up" to the client automatically. So, you get server-side and client-side validation automatically.
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by JonW
on 9/17/2009 11:05 PM

I love this MS initiative with the CDN and thinking about minimizing HTTP-request!


Regarding your code sample (My opinion):

Client side validation is in my opinion... a UI-booster (UX).

=> Therefore it should be unobtrusive.
Considering this Statement chain I think that your scripts should be placed at the bottom of the page.

Why?
See: http://developer.yahoo.com/performance/rules.html
Section "Put Scripts at the Bottom"

Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Alex
on 9/18/2009 10:40 AM

Is it possible to use MS Ajax CDN for .NET 3.5 web application and via https?
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Stephen.Walther
on 9/18/2009 12:03 PM

@Alex -- We are working on enabling SSL. We've had a lot of people ask for it.
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Jesus DeLaTorre
on 9/18/2009 2:45 PM

Hey Stephen, what about jquery ui?
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Joe Chung
on 9/18/2009 11:42 PM

Happy to see you blogging again, Stephen!
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Salman Farsi
on 9/20/2009 8:57 AM

Hi Tech Geeks,

It is one of the good achivements, I hope it will be helpful for developer community.

Thank you Microsoft and ASP.NET Team efforts...

Regards
Salman Farsi
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Michael Herndon
on 9/20/2009 9:12 AM

any chance that microsoft will add other well used libraries/plugins, like microsoft did with the jquery validation?

the syntax highlighter comes to mind cause its so widely used on various blogs, especially developer blogs.

http://alexgorbatchev.com/wiki/SyntaxHighlighter
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Andy
on 9/23/2009 8:18 PM

Love the CDN - awesome. I was wondering whether you are able to post a hack for falling back to a local version of jquery if (for reason reason) the MSFT CDN response time is >0.5 sec or something? (in the event it fails)

I really think a lot of people would find this useful ? Additionally, is something like this being built in natively or ?
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by chlamydien
on 10/1/2009 3:46 AM

we have been dependent upon google alone to serve jQuery files but after reading yours, we know that Microsoft is adding popular jQuery plugins to the CDN as well.
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Jane
on 10/1/2009 3:53 AM

Thanks for the tip Stephen now i have one for you. I see you are getting a lot of spam in your comments you can do several things to prevent spam and stop the spam from having any value should it get through. The first is adding the rel nofollow attribute to your blogs comments URLs. The second would be to strip all the URLs from comments. Also consider turning off comments after so many days of the post being made. And spend some time going through all your posts comments and removing the spam.
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by John Sinnott
on 10/2/2009 6:34 AM

Love to see a hack that flips the version between min and vs-doc depending on whether VS is in debug vs release mode
Gravatar
# re: Microsoft Ajax CDN and the jQuery Validation Library
Posted by Someone
on 10/2/2009 9:27 AM

So you put CssClass="required" to make the query pick it up as a required field. Is that correct?

Does that get in the way of using the CssClass property for Stylesheet classes?
Comments have been closed on this topic.