Created
January 12, 2012 14:25
-
-
Save bennadel/1600811 to your computer and use it in GitHub Desktop.
Some blog post??
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent | |
output="false" | |
hint="I define the application settings and event handlers."> | |
<!--- Define the application settings. ---> | |
<cfset this.name = hash( getCurrentTemplatePath() ) /> | |
<cfset this.applicationTimeout = createTimeSpan( 0, 0, 30, 0 ) /> | |
<cfset this.sessionManagement = false /> | |
<cffunction | |
name="onApplicationStart" | |
access="public" | |
returntype="boolean" | |
output="false" | |
hint="I initialize the application."> | |
<!--- | |
Let's create a collection of contacts. For simplicity | |
sake, we're not going to use a database - we're just | |
going to use an internal ColdFusion query object. | |
---> | |
<cfset application.contacts = queryNew( | |
"id, name, email, emailHash", | |
"cf_sql_integer, cf_sql_varchar, cf_sql_varchar, cf_sql_varchar" | |
) /> | |
<!--- | |
Keep a primary key for incrementing the contact IDs. | |
This will be pre-incremented every time a contact is | |
created. | |
---> | |
<cfset application.contactsPKey = 0 /> | |
<!--- Return true so the application can load. ---> | |
<cfreturn true /> | |
</cffunction> | |
<cffunction | |
name="onRequestStart" | |
access="public" | |
returntype="boolean" | |
output="false" | |
hint="I initialize the request."> | |
<!--- Check to see if we are manually resetting the application. ---> | |
<cfif structKeyExists( url, "init" )> | |
<!--- Reset the application. ---> | |
<cfset this.onApplicationStart() /> | |
</cfif> | |
<!--- Return true so the request can load. ---> | |
<cfreturn true /> | |
</cffunction> | |
</cfcomponent> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- Param the form values. ---> | |
<cfparam name="form.name" type="string" default="" /> | |
<cfparam name="form.email" type="string" default="" /> | |
<cfparam name="form.birthday" type="string" default="" /> | |
<!--- Create a collection of form validation errors. ---> | |
<cfset errors = [] /> | |
<!--- Validate the name. ---> | |
<cfif !len( form.name )> | |
<cfset arrayAppend( errors, "Please enter your name." ) /> | |
</cfif> | |
<!--- Validate the email. ---> | |
<cfif !isValid( "email", form.email )> | |
<cfset arrayAppend( errors, "Please enter a valid email." ) /> | |
</cfif> | |
<!--- Validate the birthday. ---> | |
<cfif !isNumericDate( form.birthday )> | |
<cfset arrayAppend( errors, "Please enter a valid birthday." ) /> | |
</cfif> | |
<!--- | |
Check to see if any errors were found during form | |
validation - if so, we'll have to pass them back to | |
the user. | |
---> | |
<cfif !arrayLen( errors )> | |
<!--- Form data is valid, continue to process the request. ---> | |
</cfif> | |
<!--- | |
If we made it this far, re-render the input form and display | |
the errors for the user. | |
---> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Client-Side Validation</title> | |
</head> | |
<body> | |
<form> | |
<ul id="errors" style="display: none ;"> | |
<!-- Errors go here. ---> | |
</ul> | |
Name: <input type="text" name="name" /><br /> | |
Email: <input type="text" name="email" /><br /> | |
Birthday: <input type="text" name="birthday" /><br /> | |
<input type="submit" value="Submit" /> | |
</form> | |
<script type="text/javascript" src="../jquery-1.7.js"></script> | |
<script type="text/javascript"> | |
// Bind to the form submission. | |
$( "form" ).submit( | |
function( event ){ | |
// Get the form being submitted. | |
var form = $( this ); | |
// Get the errors collection. | |
var errorsList = $( "#errors" ) | |
.empty() | |
; | |
// Validate the name. | |
if (isEmpty( form, "name" )){ | |
errorsList.append( | |
"<li>Please enter your name.</li>" | |
); | |
} | |
// Validate the email. | |
if (isEmpty( form, "email" )){ | |
errorsList.append( | |
"<li>Please enter a valid email.</li>" | |
); | |
} | |
// Validate the birthday. | |
if (isEmpty( form, "birthday" )){ | |
errorsList.append( | |
"<li>Please enter a valid birthday.</li>" | |
); | |
} | |
// Check to see if we have any errors. | |
if (errorsList.children().length){ | |
// Show the errors. | |
errorsList.show(); | |
// Cancel the form submission. | |
event.preventDefault(); | |
} | |
} | |
); | |
// I check to see if the given field is empty. | |
function isEmpty( form, fieldName ){ | |
// Return true if the field is empty. | |
return( form.find( "input[ name = '" + fieldName + "' ]" ).val() === "" ); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name=""> | |
</cffunction> | |
<html> | |
<body> | |
<a href="test">here is a link</a>. Very cool! | |
<!-- What about comments. --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Here is a Gist snippet. I'm gonna see if I can inject this | |
into the Document after it has completed. | |
--> | |
<p> | |
This was loaded from my Gist repo. Sweet!! | |
</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment