<?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>ITmeze &#187; validation</title>
	<atom:link href="http://itmeze.com/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://itmeze.com</link>
	<description>IT world served like Cyprus meze</description>
	<lastBuildDate>Sat, 26 Jun 2010 00:15:29 +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>Tips for jQuery validation plugin</title>
		<link>http://itmeze.com/2009/08/tips-for-jquery-validation-plugin/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=tips-for-jquery-validation-plugin</link>
		<comments>http://itmeze.com/2009/08/tips-for-jquery-validation-plugin/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 12:18:41 +0000</pubDate>
		<dc:creator>ITmeze</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://itmeze.com/?p=80</guid>
		<description><![CDATA[Because I currently work with a couple of projects where I extensively use jQuery framework, together with it&#8217;s plugins I decided to write a post in which I can gather all of my ideas, tips that i have arrived during my work with this library.  I am a kind of guy that has a huge [...]


Related posts:<ol><li><a href='http://itmeze.com/2009/07/firefox-3-0-411-length-required-with-jquery/' rel='bookmark' title='Permanent Link: Firefox 3.0: 411 Length required with JQuery'>Firefox 3.0: 411 Length required with JQuery</a></li>
<li><a href='http://itmeze.com/2009/09/asp-net-mvc-1-0-issue-within-htmlhelper-getmodelstatevalue-method/' rel='bookmark' title='Permanent Link: ASP.NET MVC 1.0 issue within HtmlHelper.GetModelStateValue method'>ASP.NET MVC 1.0 issue within HtmlHelper.GetModelStateValue method</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-7051816622200841";
google_ad_slot = "0915606426";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<br />
Because I currently work with a couple of projects where I extensively use jQuery framework, together with it&#8217;s plugins I decided to write a post in which I can gather all of my ideas, tips that i have arrived during my work with this library.  I am a kind of guy that has a huge mess not only on his computer&#8217;s desktop but even within project files. It happens that when I arrive a problem, and I am sure I have figured it out in some other project but it usually takes me ages to dig through all of them to find even simplest solution. That is why I decided that I really need a place for all my tips and tricks, so I can find them easily.</p>
<p>This post focuses on jQuery Validation plugin, as i think this is the second thing i add to my project &#8211; after jQuery core file <img src='http://itmeze.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . I will add more and more tips from time to time as soon as I find something worth to write down.</p>
<p>Ever worked with one of most popular <a title="jQuery Validation plugin" href="http://docs.jquery.com/Plugins/Validation" target="_blank">jQuery validation plugins</a>? This is extremely powerful and yet flexible plug-in that  makes form validation in javascript really easy.</p>
<p>Beside that fact that it already contains most popular validation rules, like checking field length, validating email or url format, it enables you to write your own &#8220;rule&#8221;.</p>
<h3>1. Adding new method</h3>
<p>Plugin supports method called &#8216;equalTo&#8217;. It is widely used &#8211; especially we request password confirmation. What is not supported is &#8216;notEqualTo&#8217; method. Not that popular but can be useful from time to time.  Let us say we have a field that needs to be different from other field, like new password has to be different than old password &#8211; maybe not the best choice but draws the picture.. OK, I guess recommended way to do that would be to add new method to jQuery validation plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  jQuery.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;notEqualTo&quot;</span><span style="color: #339933;">,</span>
                             <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> element<span style="color: #339933;">,</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">optional</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> value <span style="color: #339933;">!=</span> $<span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                             <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;This has to be different...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#change-password-form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #660066;">newPassword</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> notEqualTo<span style="color: #339933;">:</span><span style="color: #3366CC;">'#currentPassword'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                messages<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> newPassword<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> notEqualTo<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;New password has to be different than old one&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>2. Preventing double form submission</h3>
<p>Another common issue every web developer arrives sooner or later is so called &#8216;double form submit&#8217;. Or going further on &#8216;multiple form submit&#8217;. It happens when user accidentally presses submit button couple of times, causing form to be send multiple times. If we have validation plugin for our form we can make use of submitHandler method to disable button after form is submitted.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sample-form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                submitHandler<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">wasSent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">wasSent</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">':submit'</span><span style="color: #339933;">,</span> form<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Please wait...'</span><span style="color: #009900;">&#41;</span>
                                          .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'disabled'</span><span style="color: #009900;">&#41;</span>
                                          .<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>3. Turning on/off validation under certain conditions</h3>
<p>It happens quite often that we require input in certain field only when some other element is selected. I just finished making web page that allows users to subscribe to newsletters on their request.  If user selects certain check-box &#8211; it means he want to subscribe and we require from him to put valid email in other field.<br />
Otherwise, we want to keep validation turned off. To do this, we use :</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;label for=&quot;cbxSubscribe&quot;&gt; Would you like to subscribe?&lt;/label&gt;
&lt;input id=&quot;cbxSubscribe&quot; type=&quot;checkbox&quot; /&gt;
&lt;input id=&quot;txtSubscriptionEmail&quot; name=&quot;subemail&quot; /&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sample-form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    txtSubscriptionEmail<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      required<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#cbxSubscribe:checked&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As simple as that. Element &#8216;txtSubscriptionEmail&#8217; is required depending on evaluation of &#8216;#cbxSubscribe:checked&#8217; expression.</p>
<h3>4. Use CDN from Microsoft AJAX</h3>
<p>Surprise, Surprise! ASP.NET MVC 2 (since preview 2 i guess ) includes jQuery validation plugin to provide client side validation. Definitely good news!</p>
<p>Moreover, Microsoft now provides CDN for plugin:<a href="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.js"></a></p>
<ul>
<li><a href="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.js">http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.js</a></li>
<li><a href="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.min.js">http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.min.js</a></li>
</ul>
<p>As well as documentation for visual studio &#8211; this would allow IntelliSense to work under visual studio (2008 and up):</p>
<ul>
<li><a href="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate-vsdoc.js">http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate-vsdoc.js</a></li>
</ul>
<p>You should make use of CDN wherever possible. It saves you bandwidth, most probably Microsoft serves content faster and there is always a chance your visitors will have it cached already. Note that Google offers CDN for jQuery and some other libraries: <a title="google cdn" href="http://code.google.com/apis/ajaxlibs/" target="_blank">Google AJAX libraries API</a>.</p>
<p>Stay tuned for more updates&#8230;.</p>
<p>I will keep on adding more tips in future.</p>


<p>Related posts:<ol><li><a href='http://itmeze.com/2009/07/firefox-3-0-411-length-required-with-jquery/' rel='bookmark' title='Permanent Link: Firefox 3.0: 411 Length required with JQuery'>Firefox 3.0: 411 Length required with JQuery</a></li>
<li><a href='http://itmeze.com/2009/09/asp-net-mvc-1-0-issue-within-htmlhelper-getmodelstatevalue-method/' rel='bookmark' title='Permanent Link: ASP.NET MVC 1.0 issue within HtmlHelper.GetModelStateValue method'>ASP.NET MVC 1.0 issue within HtmlHelper.GetModelStateValue method</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://itmeze.com/2009/08/tips-for-jquery-validation-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
