ajaxCFC synchronous form validation example
email: *
First Name: *
Last Name: *

Existing emails:
Client Side Script
<script type='text/javascript'>_ajaxConfig = {'_cfscriptLocation':'user.cfc', '_jsscriptFolder':'../js'};</script>
<script type='text/javascript' src='../js/ajax.js'></script>

<script type="text/javascript">
	// helped variable will hold the synchronous function result; 
	// it's the bridge between the call-back function and the validation function
	var returnHelper = '';

	function validateForm(frm) {
		var valid = true;
		var msg = '';
		
		// check if email was provided
		if (frm.email.value == '') {
			valid = false;
			msg = msg + '* Email is required\n';
		}
		// check if first name was provided
		if (frm.fname.value == '') {
			valid = false;
			msg = msg + '* First Name is required\n';
		}
		// check if last name was provided
		if (frm.lname.value == '') {
			valid = false;
			msg = msg + '* Last Name is required\n';
		}

		// check if email already exists
		if (frm.email.value != '') {
			DWREngine.setAsync(false);
			DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'userExists', frm.email.value, setHelper);
			if (returnHelper == true) {
				valid = false;
				msg = msg + '* Email already exists\n';
			}
		}
		
		if (msg != '') alert(msg);
		return valid;
	}

	function setHelper(r) {
		returnHelper = r;
	}

</script>

	<fieldset>
	<legend>ajaxCFC synchronous form validation example</legend>
		<!--- onSubmit validate form --->
		<cfoutput><form name="frm" action="#script_name#" onsubmit="return validateForm(this);" method="post">
		<table border="1" frame="box" rules="groups" cellpadding="5">
			<tr><td>
				email: *<br>
				<input type="Text" name="email">
			</td></tr>
			<tr><td>
				First Name: *<br>
				<input type="Text" name="fname">
			</td></tr>
			<tr><td>
				Last Name: *<br>
				<input type="Text" name="lname">
			</td></tr>
			<tr><td align="center">
				<input type="submit" name="submit" value="create account">
			</td></tr>
		</table>
		</form></cfoutput>
	</fieldset>
	
Server Side Script
<cfcomponent extends="ajax">
	
	<cffunction name="userExists" output="no" access="private" returntype="boolean">
		<cfargument name="email" type="string" required="Yes">
		
		<cfreturn listFindNoCase(session.formValidation1,arguments.email) neq 0 />
	</cffunction>

</cfcomponent>
	
description This example shows one of the advantages for synchronous ajax. Upon form submission, a simple JavaScript code check for required fields and alerts a single message with the errors summary (try submitting a blank form).

If the email is provided, an ajax call makes sure the user doesn't already exist. Emails are stored in the session, if you try to create the same email more than once, the JavaScript alert will inform you that the email already exists.

This process is doable with asynchronous ajax, but the only way to do it is for the form validation to always return false, and the call back function re-submit the form if appropriate. This process start becoming old if you need to validate several fields, such as address, email, captcha, username, and any other you may think of.

btw, I usually wouldn't access the session from my user.cfc object, it would be accessed from some ajax facade; but for simplicity of this example, I made an exception.