<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>Wayne Graham&apos;s Blog - AJAX</title>
			<link>http://swem.wm.edu/blogs/waynegraham/index.cfm</link>
			<description>ColdFusion Development for Academic Libraries</description>
			<language>en-us</language>
			<pubDate>Sun, 22 Nov 2009 16:26:35 -0500</pubDate>
			<lastBuildDate>Thu, 21 Sep 2006 09:17:00 -0500</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>wsgrah@wm.edu</managingEditor>
			<webMaster>wsgrah@wm.edu</webMaster>
			
			
			
			
			
			<item>
				<title>Form Validation</title>
				<link>http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/9/21/Form-Validation</link>
				<description>
				
				I&apos;ve been playing with some form validation stuff for CF. I had been usign &lt;cfform&gt;, but I wanted the HTML interface to act a bit more like the Flash interface, but I don&apos;t really want to use Flash. I&apos;ve also been doing a lot more work with some of the DHTML libraries that AJAX has made popular, so I figured there had to be a relatively elegent way to do form validations with something like &lt;a href=&quot;http://prototype.conio.net/&gt;prototype&lt;/a&gt;.

I remembered seeing something on &lt;a href=&quot;http://ajaxian.com/archives/really-easy-field-validation-with-prototype&quot;&gt;Ajaxian&lt;/a&gt; about easy form validation and decided to give it a try. The article on &lt;a href=&quot;http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype/&quot;&gt;Dexagogo&lt;/a&gt; shows how they created a library to handle form-validations that doesn&apos;t require any other work than creating a form. This was just what I was looking for!

Basically, you just need the latest files from &lt;a href=&quot;http://script.aculo.us/&quot;&gt;script.aclo.us&lt;/a&gt; with the latest prototype version (the 1.5 release candidate is included in the latest script.aculo.us lib folder), and the validation library. Convienently, they&apos;re all included &lt;a href=&quot;http://tetlaw.id.au/upload/dev/validation/validation1.5.3.zip&quot;&gt;in the demo file on the site&lt;/a&gt;.

&lt;more /&gt;

To use this, you really only need prototype and the validation library (script.aculo.us adds a nice effect &amp;ndash; much like the Flash format in cfform). For me, I made these calls:

&lt;code&gt;
&lt;head&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;/scripts/scriptaculous/lib/prototype.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;/scripts/validator.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;/scripts/scriptaculous/scriptaculous.js?load=effects&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;/code&gt;

This is slightly different than the example on their page; they load the effects.js file directly, I&apos;m calling the library via script.aculo.us with the load parameter. This isn&apos;t really a big deal for one library, but it is convenient when you want to use several, but not all, of the libraries (e.g. scriptaculous.js?load=effects,dragdrop,slider).

Anyway, to actually use this, you need to create a form with an id attribute:

&lt;code&gt;
&lt;form name=&quot;feedback&quot; id=&quot;feedback&quot; action=&quot;#cgi.script_name#&quot; method=&quot;post&quot;&gt;
...
&lt;/form&gt;
&lt;/code&gt;

Now, we add some fields and use the class attribute to call the validator:

&lt;code&gt;
Name: &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; class=&quot;required&quot; /&gt;&lt;br/&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; class=&quot;required validate-email&quot; /&gt;
&lt;input type=&quot;submit&quot; /&gt;
&lt;/code&gt;

There are 11 options for use in the validation library (this is directly off their page):

&lt;ul&gt;
	&lt;li&gt;required (not blank)&lt;/li&gt;
	&lt;li&gt;validate-number (a valid number)&lt;/li&gt;
	&lt;li&gt;validate-digits (digits only)&lt;/li&gt;
	&lt;li&gt;validate-alpha (letters only)&lt;/li&gt;
	&lt;li&gt;validate-alphanum (only letters and numbers)&lt;/li&gt;
	&lt;li&gt;validate-date (a valid date value)&lt;/li&gt;
	&lt;li&gt;validate-email (a valid email address)&lt;/li&gt;
	&lt;li&gt;validate-url (a valid URL)&lt;/li&gt;
	&lt;li&gt;validate-date-au (a date formatted as; dd/mm/yyyy)&lt;/li&gt;
	&lt;li&gt;validate-currency-dollar (a valid dollar value)&lt;/li&gt;
	&lt;li&gt;validate-one-required (At least one textbox/radio element must be selected in a group)&lt;/li&gt;
&lt;/ul&gt;

This is really nice, because if you want to allow an optional field, but validate it, you can do:

&lt;code&gt;
&lt;input type=&quot;text&quot; name=&quot;email&quot; class=&quot;validate-email&quot; /&gt;
&lt;/code&gt;

There&apos;s one more piece of the pie...to call the validation library. At the bottom of your page add:

&lt;code&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	new Validation(&apos;feedback&apos;, {immediate:true});
&lt;/script&gt;
&lt;/code&gt;

The first argument is the id attribute of the form you&apos;re wanting to validate. The second tells the Validator object what to do. This particular example enables validation on each field as you leave it (which I find useful). Some of the other options are:

&lt;ul&gt;
	&lt;li&gt;stopOnFirst (boolean): Stop on the first validation failure; default: false&lt;/li&gt;
	&lt;li&gt;onSubmit (boolean): Override the default behavior of adding an even listener to the onsubmit event (set to false if you want to make sure your onsubmit method gets called no matter what); default: true&lt;/li&gt;
	&lt;li&gt;immediate (boolean): validate when the cursor leaves the field; default: false&lt;/li&gt;
	&lt;li&gt;focusOnError (boolean): place the focus on the first field with an error; default: true&lt;/li&gt;
	&lt;li&gt;useTitles (boolean): make field validators use form element title attributes as error advice message; default: false&lt;/li&gt;
	&lt;li&gt;onFormValidate (string function): call a function when the form is validated&lt;/li&gt;
	&lt;li&gt;onElementValidate (string function): call a function when an element is validated&lt;/li&gt;
&lt;/ul&gt;

What I thought was really cool was the ability to add custom validation types via an API. Say you only want folks to use capital letters for their names, you simply add a new validation type like:

&lt;code&gt;
&lt;script type=&quot;javascript&quot;&gt;
	Validation.add(&apos;validate-ucase&apos;, &apos;Please only use upper-case letters (A-Z) in this field.&apos;, function(v){
		return Validation.get(&apos;IsEmpty&apos;).test(v) || /^[A-Z]+$/.test(v);
	}
&lt;/script&gt;
&lt;/code&gt;

Want to add several? You can do that too:

&lt;code&gt;
&lt;script type=&quot;javascript&quot;&gt;
	Validation.addAllThese([
		[&apos;validate-lcase&apos;, &apos;Please only use lower-case (a-z) letters in this field&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^[a-z]+$/.test(v);
		}],
		[&apos;validate-zip&apos;, &apos;Please check your zip code&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^(\d{5})(( |-)?(\d{4}))?$/.test(v);
		}],
		[&apos;validate-phone&apos;, &apos;Please check your phone number&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^(([0-9]{3}-)|\([0-9]{3}\) ?)?[0-9]{3}-[0-9]{4}$/.test(v);
		}],
		[&apos;validate-ssn&apos;, &apos;Please check the Social Security Number. It should follow the format 999-99-9999&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^([0-9]{3}(-?)[0-9]{2}(-?)[0-9]{4})$/.test(v);
		}],
		[&apos;validate-ip&apos;, &apos;Please check the IP address&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/.test(v);
		}], 
		[&apos;validate-uuid&apos;, &apos;Please check the UUID&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{16}$/.test(v);
		}],
		[&apos;validate-guid&apos;, &apos;Please check the GUID&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^[0-9a-f]{8,8}-[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]{12,12}]$/.test(v);
		}],
		[&apos;validate-float&apos;, &apos;Please only use floating point numbers in this field&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^(\b[0-9]+\.([0-9]+\b)?|\.[0-9]+\b)$/.test(v);
		}],
		[&apos;validate-visa&apos;, &apos;Please check your credit card number&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/.test(v);
		}],
		[&apos;validate-mastercard&apos;, &apos;Please check your credit card number&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/.test(v);
		}],
		[&apos;validate-discovery&apos;, &apos;Please check your credit card number&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^6011-?\d{4}-?\d{4}-?\d{4}$/.test(v);
		}],
		[&apos;validate-amex&apos;, &apos;Please check your credit card number&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^3[4,7]\d{13}$/.test(v);
		}],
		[&apos;validate-diners&apos;, &apos;Please check your credit card number&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^3[0,6,8]\d{12}$/.test(v);
		}],
		[&apos;validate-time&apos;, &apos;Please only use time in this field&apos;, function(v){
			return Validation.get(&apos;IsEmpty&apos;).test(v) || /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/.test(v);
		}]
]);
&lt;/script&gt;
&lt;/code&gt;

This should be all of the normal items included in &lt;cfform&gt; (plus a couple extra for good measure). Now the only thing left is to make it look pretty. One of the nice things about the Flash format in &lt;cfform&gt; is that it color codes required fields with the different halo effects. To obtain a similar effect in the forms, we&apos;ll use style sheets instead.

This is a rather light stylesheet, but it&apos;ll give you something to start with (based on the default haloGreen skin):
 
 &lt;code&gt;
 input.required, textarea.required {
	border: 1px solid #ffbf2b;
 }
 input.validation-failed, textarea.validation-failed{
	border: 1px solid #ff3300;
	color: #ff3300;
 }
 input.validation-passed, textarea.validation-passed{
	border: 1px solid #00cc00;
	color: #000;
 }
 .validation-advice {
	margin: 5px 0;
	padding: 5px;
	background-color: #FF3300;
	color: #fff;
	font-weight: bold;
}
.custom-advice {
	margin: 5px 0;
	padding: 5px;
	background-color: #c8aa00;
	color: #fff;
	font-weight:bold;
}
&lt;/code&gt;
 
I made a short example of some of the validations at &lt;a href=&quot;http://swem.wm.edu/blogs/waynegraham/examples/validation/&quot;&gt;http://swem.wm.edu/blogs/waynegraham/examples/validation/&lt;/a&gt;. I have to say that I&apos;ve found this to be a bit better solution (at least for my needs) than using &lt;cfform&gt;!
				
				</description>
						
				
				<category>JavaScript</category>				
				
				<category>Web</category>				
				
				<category>ColdFusion</category>				
				
				<category>AJAX</category>				
				
				<pubDate>Thu, 21 Sep 2006 09:17:00 -0500</pubDate>
				<guid>http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/9/21/Form-Validation</guid>
				
			</item>
			
		 	
			
			
			<item>
				<title>Getting XML from MSSQL Server</title>
				<link>http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/8/21/Getting-XML-from-MSSQL-Server</link>
				<description>
				
				I&apos;ve been playing with &lt;a href=&quot;http://openrico.org/&quot;&gt;all&lt;/a&gt; &lt;a href=&quot;scriptaculous&quot;&gt;the&lt;/a&gt; &lt;a href=&quot;http://labs.adobe.com/technologies/spry/&quot;&gt;AJAX&lt;/a&gt; &lt;a href=&quot;http://dojotoolkit.org/&quot;&gt;stuff&lt;/a&gt; &lt;a href=&quot;http://developer.yahoo.com/yui/&quot;&gt;that&apos;s&lt;/a&gt; &lt;a href=&quot;http://code.google.com/webtoolkit/&quot;&gt;been&lt;/a&gt; &lt;a href=&quot;http://mochikit.com/&quot;&gt;coming&lt;/a&gt; &lt;a href=&quot;http://www.aflax.org/&quot;&gt;out&lt;/a&gt; &lt;a href=&quot;http://jquery.com/&quot;&gt;lately&lt;/a&gt;. I suppose that like a lot of folks, I was creating a query, then having a generic function that created the XML in a proxy file for the JavaScript (&lt;a href=&quot;http://ray.camdenfamily.com/index.cfm/2006/7/13/ToXML-Update&quot;&gt;Ray Camden has a really nice function for transforming a query to XML&lt;/a&gt;). 

Last week I was doing some research to find a way to do some XML searching and stumbled upon the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms190922.aspx&quot;&gt;FOR XML&lt;/a&gt; statement. I knew that most RDBMSs were capable of dealing with XML record sets, but it&apos;s been years since I&apos;ve even looked at any of the XML stuff for MSSQL. 

The FOR XML statement returns a query result and transforms rows into XML elements. There are three arguments that this can take:

&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms175140.aspx&quot;&gt;RAW&lt;/a&gt;: Transforms each row into an element with a generic identifier (&amp;lt;row/&gt;) as the element tag.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms188273.aspx&quot;&gt;AUTO&lt;/a&gt;: Returns the results in a simple nested XML tree&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms175140.aspx&quot;&gt;EXPLICIT&lt;/a&gt;: Allows you to define the XML tree returned&lt;/li&gt;
&lt;/ul&gt;
				 [More]
				</description>
						
				
				<category>ColdFusion</category>				
				
				<category>AJAX</category>				
				
				<category>XML</category>				
				
				<pubDate>Mon, 21 Aug 2006 11:49:00 -0500</pubDate>
				<guid>http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/8/21/Getting-XML-from-MSSQL-Server</guid>
				
			</item>
			
		 	
			</channel></rss>