<?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>Kevin Grad &#187; jquery</title>
	<atom:link href="http://couchware.ca/www/kev/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://couchware.ca/www/kev</link>
	<description>Designer, Programmer and Co-Founder</description>
	<lastBuildDate>Fri, 30 Apr 2010 20:33:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using custom attributes for validation</title>
		<link>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/</link>
		<comments>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 18:20:39 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[selectors]]></category>
		<category><![CDATA[servlet]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=66</guid>
		<description><![CDATA[I was hired a couple of months ago to build a validation system for a legacy web-app built on Java Servlet technology. The app was a gigantic system with many existing web forms.

The job requirements included extendability, maintenance, minimal overhead, and it had to work with the current code, i.e. I couldn&#8217;t go and redesign [...]]]></description>
			<content:encoded><![CDATA[<p>I was hired a couple of months ago to build a validation system for a legacy web-app built on <a href="http://java.sun.com/products/servlet/">Java Servlet technology</a>. The app was a gigantic system with many existing web forms.</p>
<p><span id="more-66"></span></p>
<p>The job requirements included extendability, maintenance, minimal overhead, and it had to work with the current code, i.e. I couldn&#8217;t go and redesign the entire system. There were already hundreds of forms in the system with thousands of fields requiring validation. My first task was to explore the different possibilities. I came up with a few various schemes for both client side and server side validations. I opted to go with a client side solution for various reasons, including the fact that I could update the page with any error messages without ever needing to contact the server.</p>
<p>The major problem that I had to address was how to insert my validation functions within the already created web forms in an easy, extensible way. At the time I had been playing around with a JavaScript library known as <a href="http://jquery.com">jQuery</a>. I had very little experience with it, but it seemed very interesting. Cam had also recently begun using jQuery and was big on the library.</p>
<p>One of jQueries big features is easy manipulation of the DOM tree. jQuery ingeniously makes use of <a href="http://www.w3.org/TR/CSS2/selector.html">CSS Selectors</a> to wrap a group of elements in a jQuery wrapper. If you have never used CSS selectors, I HIGHLY recommend taking a look at them.</p>
<p>Using jQuery, the following simple code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.foo'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>returns an array of all DOM elements with the attribute class=&#8221;foo&#8221;. You can then iterate over this wrapped set in a number of ways. This is immensely powerful. Additionally, jQuery will recognize custom attributes.</p>
<p>The code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[required=true]'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>will return all input elements with a custom attribute required=&#8221;true&#8221;. What this means is, if you have in your HTML a text field like this:</p>
<p>&lt;input type=&#8221;text&#8221; required=&#8221;true&#8221;&gt;</p>
<p>The jQuery code above will find that element and return it in a wrapped jQuery array. I used this as the basis for my validation system.</p>
<p>What I did, was define a set of custom attributes, and a set of JavaScript functions based on requirements given to me by the hiring company. Then I created a validate() function which sequentially called all of my JavaScript functions. This validate function was meant to be called in an onSubmit() but could be called anytime.</p>
<p>If you ever wanted a field to be validated, you simply added my custom attributes to its HTML markup. So for example, if you wanted a field to be marked required, you simply went to that field and added required=&#8221;true&#8221;. If you wanted a field to be checked for a valid integer, you went to the field and added type=&#8221;integer&#8221;.</p>
<p>The beauty of this approach is that it allows a field to be marked with an arbitrary number of validations. And removing validation is as easy as removing the custom attribute. Additionally, if you add a new function to the set of JavaScipt functions, all you need to do to add it to a field is add the new custom attribute.</p>
<p>The way this works is, when the person submits the form, the validate function is called. The validate function uses jQuery to parse the DOM tree and iterate over the results. So within the validate function, the first function looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[required=yes]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>    
&nbsp;
  <span style="color: #006600; font-style: italic;">// validate the required strings.</span>
  <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> validateRequired<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'field_name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// If we had an error, concatenate it into the error string and add the errorCase</span>
  errorStr <span style="color: #339933;">+=</span> errorCheck<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What this did was, it searched the page for any input elements with the custom attribute required=&#8221;yes&#8221;. It then iterated over that set and passed the value into the JavaScript function validateRequired() which checked whether the field was non-empty. It then returned an error string from a function that properly formatted error messages.</p>
<p>Each method concatenated an error string when there were any errors, and at the end of the validate() function if there were any error messages it printed them to the screen in a custom div. Otherwise, it allowed the submit to go through.</p>
<p>This was done for every type of validation, even if there was no elements with that custom tag within the current page. This makes it easy to simply add new validations on the fly. Also, all the JavaScript functions are in a central place and are easy to modify/update and have it filter through the system automatically.</p>
<p>This solution worked perfectly as it was easy to implement and fit the requirements perfectly. The client was pleased and everything worked out.</p>
<p>This may not be the way I would design the validation system today, but it was an excellent choice at the time.</p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/02/02/using-custom-attributes-for-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
