<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn Boland &#187; jQuery</title>
	<atom:link href="http://blogs.taiga.nl/martijn/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.taiga.nl/martijn</link>
	<description>New adventures in .NET</description>
	<lastBuildDate>Tue, 27 Apr 2010 20:51:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Validation in ASP.NET MVC &#8211; part 4: example with server-side and client-side validation</title>
		<link>http://blogs.taiga.nl/martijn/2008/12/08/validation-in-aspnet-mvc-part-4-example-with-server-side/</link>
		<comments>http://blogs.taiga.nl/martijn/2008/12/08/validation-in-aspnet-mvc-part-4-example-with-server-side/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 21:18:15 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sample]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/archive/2008/12/08/validation-in-asp.net-mvc-part-4-example-with-server-side.aspx</guid>
		<description><![CDATA[This is part 4 of a series of posts. See also: Validation in ASP.NET MVC &#8211; part 1: basic server-side validation Validation in ASP.NET MVC &#8211; part 2: custom server-side validation Validation in ASP.NET MVC &#8211; part 3: client-side validation with jQuery validation In the previous posts, I described how we implemented model-based validation on [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 4 of a series of posts. See also:</p>
<ul>
<li><a href="http://blogs.taiga.nl/martijn/2008/11/26/validation-in-asp.net-mvc-part-1-basic-server-side-validation">Validation in ASP.NET MVC &#8211; part 1: basic server-side validation</a></li>
<li><a href="http://blogs.taiga.nl/martijn/2008/11/27/validation-in-asp.net-mvc-part-2-custom-server-side-validation">Validation in ASP.NET MVC &#8211; part 2: custom server-side validation</a></li>
<li><a href="http://blogs.taiga.nl/martijn/2008/12/08/validation-in-asp.net-mvc-part-3-client-side-validation-with">Validation in ASP.NET MVC &#8211; part 3: client-side validation with jQuery validation</a></li>
</ul>
<p>In the previous posts, I described how we implemented model-based validation on the server-side in <a href="http://cuyahoga-project.org" target="_blank">Cuyahoga</a> with ASP.NET MVC and as the icing on the cake, we also added some client-side validation (also model-based).</p>
<p>Now, everything can be found in the <a href="https://cuyahoga.svn.sourceforge.net/svnroot/cuyahoga/trunk/" target="_blank">Cuyahoga SVN sources</a> but I have to agree that it can be a daunting task to check out the complete sources and find all pieces of the validation puzzle. Therefore, I&#8217;ve created a little sample application with everything from the previous posts combined.</p>
<p><a href="http://blogs.taiga.nl/images/blogs_taiga_nl/martijn/WindowsLiveWriter/Val.NETMVCpart4examplewithserversideandc_13761/validation-sample-app_2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" src="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/Val.NETMVCpart4examplewithserversideandc_13761/validation-sample-app_thumb.png" border="0" alt="validation-sample-app" width="644" height="484" /></a></p>
<p>Hopefully, this will make things a little bit clearer. <a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/MvcValidationSample.zip" target="_blank">You can download the sample app here</a>. Visual Studio 2008 and <a href="http://www.microsoft.com/Downloads/details.aspx?FamilyID=a24d1e00-cd35-4f66-baa0-2362bdde0766&amp;displaylang=en" target="_blank">ASP.NET MVC Beta</a> are required.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2008/12/08/validation-in-aspnet-mvc-part-4-example-with-server-side/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Validation in ASP.NET MVC &#8211; part 3: client-side validation with jquery validation</title>
		<link>http://blogs.taiga.nl/martijn/2008/12/08/validation-in-aspnet-mvc-part-3-client-side-validation-with/</link>
		<comments>http://blogs.taiga.nl/martijn/2008/12/08/validation-in-aspnet-mvc-part-3-client-side-validation-with/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 15:09:19 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Cuyahoga]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/archive/2008/12/08/validation-in-asp.net-mvc-part-3-client-side-validation-with.aspx</guid>
		<description><![CDATA[This is part 3 of a series of posts. See also: Validation in ASP.NET MVC &#8211; part 1: basic server-side validation Validation in ASP.NET MVC &#8211; part 2: custom server-side validation In the first two parts, I showed how you can perform validation on the server side with Castle Validation attributes and extend that model [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 3 of a series of posts. See also:</p>
<ul>
<li><a href="http://blogs.taiga.nl/martijn/2008/11/26/validation-in-asp.net-mvc-part-1-basic-server-side-validation">Validation in ASP.NET MVC &#8211; part 1: basic server-side validation</a></li>
<li><a href="http://blogs.taiga.nl/martijn/2008/11/27/validation-in-asp.net-mvc-part-2-custom-server-side-validation">Validation in ASP.NET MVC &#8211; part 2: custom server-side validation</a></li>
</ul>
<p>In the first two parts, I showed how you can perform validation on the server side with Castle Validation attributes and extend that model with custom validation logic. With this, we have everything we need to properly validate our model.</p>
<p>For a better user experience though, it would also be nice that we could re-use (part of) our validation logic on the client-side. Luckily, lots of others have already looked into this and the only thing I had to do was to throw everything together and stir it a little bit <img src='http://blogs.taiga.nl/martijn/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>The ingredients are:</p>
<ul>
<li><a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank">The jQuery validation plugin</a> (an obvious choice, since we were already using jQuery in <a href="http://cuyahoga-project.org" target="_blank">Cuyahoga</a>);</li>
<li><a href="http://svn.castleproject.org:8080/svn/castle/trunk/MonoRail/Castle.MonoRail.Framework/Helpers/ValidationStrategy" target="_blank">Monorail client-side code generator for jQuery validation</a> (originally contributed by Gildas, but also many credits to the Castle Monorail guys for creating such a flexible infrastructure);</li>
<li><a href="http://blog.codeville.net/2008/04/30/model-based-client-side-validation-for-aspnet-mvc/">Model-based Client-side Validation for ASP.NET MVC</a> (very inspiring blog post by Steve Sanderson).</li>
</ul>
<h3>How it works</h3>
<h3><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/Val.NETMVCpart3clientsidevalidationwithj_CA52/client-side-validation-1_2.png"><img style="border: 0pt none;" src="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/Val.NETMVCpart3clientsidevalidationwithj_CA52/client-side-validation-1_thumb.png" border="0" alt="client-side-validation-1" /></a></h3>
<ol>
<li>The client-side validation is called via an HtmlHelper extension method ClientSideValidation:<br />
&lt;%= Html.ClientSideValidation(ViewData.Model, &#8220;my_form_id&#8221;)%&gt;;</li>
<li>The HtmlHelper extension requests an instance of a BrowserValidationEngine that returns the client script for validation;</li>
<li>BrowserValidationEngine has a reference to an IValidatorRegistry instance that returns all (Castle) validators for the given model;</li>
<li>For each validator, the referenced IBrowserValidatorProvider generates the appropriate client script;</li>
<li>Finally, all generated client script code is combined and sent to the browser in a single javascript block.</li>
</ol>
<p>The existing Monorail codebase proved to be of great value and could be used almost seamlessly. The only difference is the way the client code is generated. Originally, the validators generated css class attributes for the Monorail form helpers, but we don&#8217;t have those with ASP.NET MVC, so all client code is generated as jQuery validation rules.</p>
<p>Below, you can see the output of the validation helper for the new user form in Cuyahoga:</p>
<div style="border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">&lt;script type=<span style="color: #006080">"text/javascript"</span>&gt;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">$(document).ready(function() {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">    jQuery.validator.addMethod(<span style="color: #006080">'notEqualTo'</span>, function(<span style="color: #0000ff">value</span>, element, param) { <span style="color: #0000ff">return</span> <span style="color: #0000ff">value</span> != jQuery(param).val(); }, <span style="color: #006080">'Must not be equal to {0}.'</span> );</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    jQuery.validator.addMethod(<span style="color: #006080">'greaterThan'</span>, function(<span style="color: #0000ff">value</span>, element, param) { <span style="color: #0000ff">return</span> ( IsNaN( <span style="color: #0000ff">value</span> ) &amp;&amp; IsNaN( jQuery(param).val() ) ) || ( <span style="color: #0000ff">value</span> &gt; jQuery(param).val() ); }, <span style="color: #006080">'Must be greater than {0}.'</span> );</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">    jQuery.validator.addMethod(<span style="color: #006080">'lesserThan'</span>, function(<span style="color: #0000ff">value</span>, element, param) { <span style="color: #0000ff">return</span> ( IsNaN( <span style="color: #0000ff">value</span> ) &amp;&amp; IsNaN( jQuery(param).val() ) ) || ( <span style="color: #0000ff">value</span> &lt; jQuery(param).val() ); }, <span style="color: #006080">'Must be lesser than {0}.'</span> );</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    jQuery.validator.addMethod(<span style="color: #006080">'numberNative'</span>, function(<span style="color: #0000ff">value</span>, element, param) { <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:\,\d+)?$/.test(<span style="color: #0000ff">value</span>); }, <span style="color: #006080">'Not a valid number.'</span> );</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">    jQuery.validator.addMethod(<span style="color: #006080">'simpleDate'</span>, function(<span style="color: #0000ff">value</span>, element, param) { <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.optional(element) || /^\d{1,2}\-\d{1,2}\-\d{4}$/.test(<span style="color: #0000ff">value</span>); }, <span style="color: #006080">'Not a valid date.'</span> );</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">    $(<span style="color: #006080">"#userform"</span>).validate({</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">        rules : {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            UserName: {  rangelength: [1, 50] , required: <span style="color: #0000ff">true</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            Password: {   required: <span style="color: #0000ff">true</span> , rangelength: [5, 50] , equalTo: <span style="color: #006080">"#PasswordConfirmation"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            PasswordConfirmation: {   equalTo: <span style="color: #006080">"#Password"</span> , rangelength: [5, 50] , required: <span style="color: #0000ff">true</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            FirstName: { rangelength: [1, 100] },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            LastName: { rangelength: [1, 100] },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            Email: {   required: <span style="color: #0000ff">true</span> , rangelength: [1, 100] , email: <span style="color: #0000ff">true</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            Website: { rangelength: [1, 100] }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">        },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">        messages : {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            UserName: {  rangelength: <span style="color: #006080">"The username must be between 1 and 50 characters"</span> , required: <span style="color: #006080">"The username may not be empty"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            Password: {   required: <span style="color: #006080">"The password may not be empty"</span> , rangelength: <span style="color: #006080">"The password must contain at least 5 characters"</span> , equalTo: <span style="color: #006080">"The password must be the same as the confirmation password"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            PasswordConfirmation: {   equalTo: <span style="color: #006080">"The password must be the same as the confirmation password"</span> , rangelength: <span style="color: #006080">"The password must contain at least 5 characters"</span> , required: <span style="color: #006080">"The password confirmation may not be empty"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            FirstName: { rangelength: <span style="color: #006080">"The firstname must be between 1 and 100 characters"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            LastName: { rangelength: <span style="color: #006080">"The lastname must be between 1 and 100 characters"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">            Email: {   required: <span style="color: #006080">"E-mail address may not be empty"</span> , rangelength: <span style="color: #006080">"E-mail address must be between 1 and 100 characters"</span> , email: <span style="color: #006080">"Invalid e-mail address"</span> },</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">            Website: { rangelength: <span style="color: #006080">"The website url must be between 1 and 100 characters"</span> }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">        }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">    });</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">});</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">&lt;/script&gt;</pre>
</div>
</div>
<p>which results in this:</p>
<p><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/Val.NETMVCpart3clientsidevalidationwithj_CA52/client-side-validation-2_2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" src="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/Val.NETMVCpart3clientsidevalidationwithj_CA52/client-side-validation-2_thumb.png" border="0" alt="client-side-validation-2" width="644" height="484" /></a></p>
<h3>Summarizing validation</h3>
<p>In this series of posts, I showed how we deal with validation in Cuyahoga and ASP.NET MVC. Personally, I think the nicest part of it is that we have our validation rules centralized and we only have to add one line of code to the view to enable client-side validation.</p>
<p>The code can be found in <a href="https://cuyahoga.svn.sourceforge.net/svnroot/cuyahoga/trunk/" target="_blank">Cuyahoga SVN</a> and more specifically in the <a href="https://cuyahoga.svn.sourceforge.net/svnroot/cuyahoga/trunk/Core/Validation/" target="_blank">validation</a> <a href="https://cuyahoga.svn.sourceforge.net/svnroot/cuyahoga/trunk/Cuyahoga.Web.Mvc/Validation/" target="_blank">sections</a> and the <a href="https://cuyahoga.svn.sourceforge.net/svnroot/cuyahoga/trunk/Web/Manager/" target="_blank">ASP.NET MVC manager</a>. I&#8217;ll try to make a standalone sample project with all the validation stuff somewhere in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2008/12/08/validation-in-aspnet-mvc-part-3-client-side-validation-with/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
