<?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>Marcin Obel&#187; jQuery</title>
	<atom:link href="http://marcinobel.com/index.php/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://marcinobel.com</link>
	<description>.NET, ASP.NET MVC, jQuery, Ruby, Ruby on Rails, Test Driven Development, Agile</description>
	<lastBuildDate>Thu, 20 May 2010 14:25:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Metadata based validation with jQuery</title>
		<link>http://marcinobel.com/index.php/metadata-based-validation-with-jquery/</link>
		<comments>http://marcinobel.com/index.php/metadata-based-validation-with-jquery/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 19:36:36 +0000</pubDate>
		<dc:creator>Marcin Obel</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://marcinobel.com/?p=75</guid>
		<description><![CDATA[
Recently I spent some time prototyping my own validation for ASP.NET MVC. Why did not I reuse any of existing solutions? This is a story for another post so I did not want to bring this topic up now. One of the things which I had to figure out during prototyping was how to implement [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://marcinobel.com/wp-content/uploads/2009/10/jquery.validation.png"><img class="alignleft size-full wp-image-76" title="jQuery Validation enabled form" src="http://marcinobel.com/wp-content/uploads/2009/10/jquery.validation.png" alt="jQuery Validation enabled form" width="353" height="498" /></a></p>
<p>Recently I spent some time prototyping my own validation for <a href="http://www.asp.net/mvc/" target="_blank"><strong>ASP.NET MVC</strong></a>. Why did not I reuse any of existing solutions? This is a story for another post so I did not want to bring this topic up now. One of the things which I had to figure out during prototyping was how to implement client side validation. Of course I wanted to utilize capabilities of some well known <strong>JavaScript</strong> framework (no I am not so crazy to write it for my own). I made small evaluation comparing available options and at the end I decided to incorporate <a href="http://jquery.com/" target="_blank"><strong>jQuery</strong></a> and its <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank"><strong>Validation</strong></a> plugin. One of the things which were crucial to my decision was ability of this framework to use metadata stored as <strong>JSON</strong> in class attribute of <strong>HTML</strong> element. The concept of metadata available in<strong> jQuery</strong> is amazing and gives possibility to describe validation rules for specified input control inside itself. In my case this was very important feature because I wanted to render validation using my custom HtmlHelper extensions together with corresponding input controls. Second thing which convinced me to <strong>jQuery Validation</strong> was easy setup which I want to show you now.</p>
<p>If you want to use <strong>jQuery Validation</strong> in your project first of all you will need of course <strong>jQuery</strong> library which can be downloaded <a href="http://jquery.com/" target="_blank"><strong>here</strong><strong></strong></a>, <strong>jQuery Metadata</strong> plugin which is available <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank"><strong>here</strong></a> and <strong>jQuery Validation</strong> plugin downloadable from <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank"><strong>here</strong></a>.</p>
<p>When you collected all required prerequisites you have to add reference to them in your <strong>HTML</strong>. Simply add code shown below to the body of <em><strong><head /></strong></em> tag but remember to ensure that paths lead to place where <strong>JavaScript</strong> files are located.</p>
<pre class="brush: xml;">
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.validate.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.metadata.js&quot;&gt;&lt;/script&gt;
</pre>
<p>When<strong> jQuery</strong> libraries are already where they should be you can specify validation rules using metadata. In order to do it extend your input control tag with <em><strong>class</strong></em> attribute and set its value to <strong><em>{required:true}</em></strong>. This will tell <strong>Validation</strong> plugin that this specified input control should contain a value before it can be send back to server. Of course this is very simple rule but below I have listed few more, complex definitions.</p>
<pre class="brush: xml;">
&lt;input class=&quot;{required:true, maxlength:100, messages:{required:'This field is required.', maxlength:'This field can contain maximum 100 characters.'}}&quot; /&gt;
&lt;input class=&quot;{maxlength:50, email:true, equalTo:'#Email',  messages:{required:'This field is required.', maxlength:'This field can contain maximum 100 characters.', email:'This is not a valid email.', equalTo:'Value entered in this field should equal to value of Email field.'}}&quot; /&gt;
</pre>
<p>Listing below shows this validation metadata in wider context.</p>
<pre class="brush: xml;">
&lt;form id=&quot;MyForm&quot; method=&quot;post&quot; action=&quot;/Registration&quot;&gt;
    &lt;label for=&quot;DisplayName&quot;&gt;Username:&lt;/label&gt;
    &lt;input id=&quot;DisplayName&quot; class=&quot;{required:true, maxlength:100, messages:{required:'This field is required.', maxlength:'This field can contain maximum 100 characters.'}}&quot; type=&quot;text&quot; name=&quot;DisplayName&quot; /&gt;
    &lt;input value=&quot;Submit!&quot; type=&quot;submit&quot; /&gt;
&lt;/form&gt;
</pre>
<p>At the end when you have defined all validation rules only thing you should do is adding following code after closing <em><strong></strong></em> tag and that is it.</p>
<pre class="brush: xml;">
&lt;script type=&quot;text/javascript&quot;&gt;
    $(&quot;#MyForm&quot;).validate({
        errorElement: &quot;span&quot;
    });
&lt;/script&gt;
</pre>
<p>Now when you will click <em>Submit!</em> button your form will be validated and in case of errors apropriate messages will be displayed below corresponding input control.</p>
<p>Because I know that described example can be not enough to fully understand how all it works I have prepared more complex, example form which can be downloaded from here: <a href="http://marcinobel.com/wp-content/uploads/2009/10/jQuery.Validation.Sample.zip"><strong>download</strong></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://marcinobel.com/index.php/metadata-based-validation-with-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
