<?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>Garrick Cheung &#187; form validation</title>
	<atom:link href="http://www.garrickcheung.com/tag/form-validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.garrickcheung.com</link>
	<description>Sharing what I know and learn about CSS, MooTools, Javascript, PHP and etc.</description>
	<lastBuildDate>Fri, 30 Sep 2011 05:16:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Clientcide: New Form Validators and Documented Beta Code</title>
		<link>http://www.garrickcheung.com/mootools/clientcide-new-form-validators-and-documented-beta-code/</link>
		<comments>http://www.garrickcheung.com/mootools/clientcide-new-form-validators-and-documented-beta-code/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 20:22:56 +0000</pubDate>
		<dc:creator>Garrick</dc:creator>
				<category><![CDATA[MooTools]]></category>
		<category><![CDATA[form validation]]></category>
		<category><![CDATA[input validation]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://www.garrickcheung.com/?p=176</guid>
		<description><![CDATA[Aaron Newton has released some new form validators (with help from contributor Chafik Barbar) and documentation for his beta code. Everyone has to deal with form validators one way or another. I&#8217;ve posted on one from MooTools.Floor, but this is a different way of doing things. The FormValidator / InputValidator Code There are two ways [...]]]></description>
			<content:encoded><![CDATA[<p>Aaron Newton has <a href="http://www.clientcide.com/code-releases/new-form-validators-beta-code-is-now-documented/" target="_blank">released some new form validators (with help from contributor Chafik Barbar) and documentation for his beta code</a>. Everyone has to deal with form validators one way or another. I&#8217;ve <a href="http://www.garrickcheung.com/mootools/mootoolsfloor-does-form-validation-right/">posted on one from MooTools.Floor</a>, but this is a different way of doing things.<span id="more-176"></span></p>
<h3>The FormValidator / InputValidator Code</h3>
<p>There are two ways to set up form validators. The first is to use InputValidator.</p>
<blockquote><p>This class contains functionality to test a field for various criteria and also to generate an error message when that test fails.</p></blockquote>
<p>It takes two arguments, the class name that is applied on the element and options to test against and errorMsg to display.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">//html code
&lt;input type=&quot;text&quot; name=&quot;firstName&quot; class=&quot;required&quot; id=&quot;firstName&quot; /&gt;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//simple validator</span>
<span style="color: #003366; font-weight: bold;">var</span> isEmpty <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> InputValidator<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'required'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    errorMsg<span style="color: #339933;">:</span> <span style="color: #3366CC;">'This field is required.'</span><span style="color: #339933;">,</span>
    test<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>field<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
isEmpty.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//true if empty</span>
isEmpty.<span style="color: #660066;">getError</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">//returns &quot;This field is required.&quot;</span></pre></td></tr></table></div>

<p>Then there&#8217;s FormValidator.</p>
<blockquote><p>Evalutes an entire form against all the validators that are set up, displaying messages and returning a true/false response for the evaluation of the entire form.</p></blockquote>
<p>You can set it up with custom validators using the arguments you would in InputValidator too, for all instances of the FormValidator or just the instance of the FormValidator.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//add a validator for ALL instances</span>
FormValidator.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'isEmpty'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    errorMsg<span style="color: #339933;">:</span> <span style="color: #3366CC;">'This field is required'</span><span style="color: #339933;">,</span>
    test<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<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>element.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//this validator is only available to this single instance</span>
<span style="color: #003366; font-weight: bold;">var</span> myFormValidatorInstance <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FormValidator<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myform'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myFormValidatorInstance.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doesNotContainTheLetterQ'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    errorMsg<span style="color: #339933;">:</span> <span style="color: #3366CC;">'This field cannot contain the letter Q!'</span><span style="color: #339933;">,</span>
    test<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!</span>element.<span style="color: #660066;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/q/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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></td></tr></table></div>

<p>At first glance, I didn&#8217;t really like this setup of the form validator. It felt clunky; I would have to create many custom validators. But I realized you can add custom validators globally or for instances and I wouldn&#8217;t have to re-build them again. I <u>think</u> this is actually smaller than MooTools.Floor&#8217;s validator. </p>
<p>Thank you, Aaron and Chafik. Now we&#8217;ll know how to use/test the beta code and we won&#8217;t have to write extra validators. <img src='http://www.garrickcheung.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.garrickcheung.com/mootools/clientcide-new-form-validators-and-documented-beta-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MooTools.Floor Does Form Validation Right!</title>
		<link>http://www.garrickcheung.com/mootools/mootoolsfloor-does-form-validation-right/</link>
		<comments>http://www.garrickcheung.com/mootools/mootoolsfloor-does-form-validation-right/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 06:05:59 +0000</pubDate>
		<dc:creator>Garrick</dc:creator>
				<category><![CDATA[MooTools]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[form validation]]></category>
		<category><![CDATA[MooTools.Floor]]></category>

		<guid isPermaLink="false">http://www.garrickcheung.com/?p=82</guid>
		<description><![CDATA[We&#8217;ve all dealt with form validation one way or another and I didn&#8217;t find one to my liking. I was mulling over the idea of writing my own for a while and then I read Ajaxian&#8217;s blog today about a really awesome MooTools form validation class from MooTools.Floor. MooTools.Floor actually announced their FormCheck v1.4 form [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all dealt with form validation one way or another and I didn&#8217;t find one to my liking. I was mulling over the idea of writing my own for a while and then I read <a href="http://ajaxian.com/archives/formcheck-great-form-validation-for-mootools" target="_blank">Ajaxian&#8217;s blog today about a really awesome MooTools form validation class</a> from <a href="http://mootools.floor.ch/en/" target="_blank">MooTools.Floor</a>. <a href="http://mootools.floor.ch/en/" target="_blank">MooTools.Floor</a> actually <a href="http://mootools.floor.ch/blog/?p=23" target="_blank">announced</a> their FormCheck v1.4 form validation class in July 25, 2008.<span id="more-82"></span></p>
<p>MooTools.Floor was nice enough to post <a href="http://mootools.floor.ch/docs/formcheck/files/formcheck-js.html" target="_blank">documentation</a> for the class, though there&#8217;s a lot of options to go through.</p>
<p>Errors appear in tools tips, form submission via ajax is available, error messages can be customizable, the options are endless! Like I said, there are lots of options.</p>
<p>To set whether an input, textarea, or select element needs to validate, you give the element a class property:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;text&quot; class=&quot;validate['required','length[4, -1]','differs[email]','digit']&quot; name=&quot;test_field&quot; value=&quot;&quot; /&gt;</pre></td></tr></table></div>

<p>To set up a form, you instantiate the MooTools class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myCheck <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FormCheck<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form_id'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    tipsClass <span style="color: #339933;">:</span> <span style="color: #3366CC;">'tips_box'</span><span style="color: #339933;">,</span>
        display <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            scrollToFirst <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        alerts <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            required <span style="color: #339933;">:</span> <span style="color: #3366CC;">'This field is ablolutely required! Please enter a value'</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></td></tr></table></div>

<p><a href="http://www.garrickcheung.com/wp-content/uploads/formcheck.jpg" target="_blank"><img src="http://www.garrickcheung.com/wp-content/uploads/formcheck.jpg" alt="Screenshot of MooTools.Floor's FormCheck v1.4" style="width:455px;" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.garrickcheung.com/mootools/mootoolsfloor-does-form-validation-right/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

