<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>
<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>