ColdFusion Tips and Tutorials

ColdFusion Tips and Tutorials. Tips on ColdFusion, AJAX, CSS, JavaScript, HTML, Design, and more.

CFUnited Developer Conference 2010
Use this code TIPSCUST to get $100 off your registration @ CFUnited! We'll see you There!


ColdFusion Tips
Page 1 2 3
148 ColdFusion, Ajax, FuseBox, Tips, and Tut
147 Included Point of View
146 Javascript - OnFocus
145 Nathan's Rules of Professional Web Desig
144 Universal Server-Side Check | Bandwidth
143 Meeting Schedule | Identification Sessio
142 Breaking Frames Without Javascript
141 Unreal Forms
140 Screen Resolution
139 Human Help
138 Better Server-Side Validation
137 Automatic Server-Side Validation
136 Regular Expression Laboratory,ColdFusion
135 Rank-Ordered Site Search
134 Building Cryptograms
133 Well-Formed Includes
132 Grouping Families for Visits
131 Display Families on a Map Grid
130 Slide Shows
129 Determine Your Database Engine
128 ColdFusion in Context: Maxlength Lies
127 Something Extra
126 Parsing Database Structure from Data Def
125 Valid Values Maintenance
124 Print 1
123 Hide Session Id
122 Downsizing Data to Access
121 Time to Load a Page, FuseBox 4
120 Order and Rank by Subset
119 Warn through E-mail & Update on Paste Sp
118 Paste Spreadsheets, ColdFusion Component
117 Review Files Having Fixed-Length Fields
116 Organized Help
115 Sequence Slider
114 Bad Bits
113 Logical Deduction
112 Whiteout
111 Forced Navigation
110 Managing Permissions
109 Time Travel
108 Test First
107 Get Distance Between Map Coordinates
106 Validating Checkboxes
105 Matrix Manipulation
104 Field Help
103 Fake Object Not Found
102 Rank Order Correlation Coefficient
101 From Calling Pairs to Calling Tree
100 Posting Notice
99 Logout Persuasion
98 Release Session Memory
97 Use Identically Named Fields
96 Web Bug
95 Password Generation
94 Core Queries
93 Use CFFTP
92 Insert, Update, and Delete
91 Stack
90 T-Value
89 Bulk Data Entry and E-mail Validation
88 Quick Reset
87 Design 1
86 Use CFFTP
85 Support Login with AutoPost
84 Login and Site Protection
83 XY Graphs in a Graphing Calculator
82 Read Encrypted Files
81 Showing Progress
80 Frugal Cross-Browser Javascript
79 Tabbed Folders
Page 1 2 3



Custom Search
ColdFusion TIPS PLUS


Issue 00136 http://www.cftipsplus.com

I. My Comments

II. ColdFusion In Context: Regular Expression Laboratory
By R. Martin Ladner
martin.ladner@charter.net

III. ColdFusion and File Maker
programmer@donomite.com


I. Comments:

Been Busy with a lot of life. Things are going good. Close on my house is on Dec. 12th.



Keep Coding,
Nathan Stanford
http://www.cftipsplus.com

If you have suggestions for articles send them to us.
If you would like to write for cftipsplus.com
send us an email to:

NathanS<at>nsnd.com

IF YOU WANT TO BE AN AUTHOR SEND IN YOUR COLDFUSION TIPS.

Remember this is a great way to get your name known in the
ColdFusion Community.




II. ColdFusion in Context: Regular Expression Laboratory
By R. Martin Ladner
martin.ladner@charter.net



Regular expressions let you perform complex pattern matching using a single line of "code" to confirm input formats and even ranges. Regular expressions are a feature of a number of languages, for example, perl, javascript, and ColdFusion. The syntax used for this feature is similar but not exactly the same across languages.

Suppose you want to borrow regular expressions you find in javascript, perl, or other places for use with ColdFusion. Alas, not only will you need to convert them to the syntax supported by ColdFusion, but you'll need to test them to be sure they meet your needs or at least do what the author apparently thinks they'll do.

This tip builds a regular expression laboratory that makes the most common conversions for you and then lets you test the result.

Build the Script

Put this code in Regex.cfm. Begin with a javascript routine to do the conversion. Add leading and trailing slashes so that (as is common in the syntax used by perl and javascript) this script will know where the pattern begins and ends. The slashes will already be present in code examples but may be "understood" in catalogs and discussions. Hence, supplying them internally if they're not present saves typing and reduces frustration.

<script language="javascript"> // Convert javascript regular expression to ColdFusion regex syntax function demo(TheForm) {
Workval=TheForm.JSExpression.value;
// Overview. // Before any other changes, add leading and trailing slashes if not present. // For each mask type, fix leading, negated, and remaining masks. // As the last change, remove leading and trailing slashes.
// Add leading and trailing slashes if not present. if (Workval.search(/^\//) < 0) { Workval='/'+Workval;
}
if (Workval.search(/\/$/) < 0) {
Workval=Workval+'/';
}

This script converts the single-letter shorthand for many character masks to a formal representation: [[:digit:]] for \d, [[:alnum:]] for \w (alphanumeric), and so forth. For each shorthand representation, it confronts two problems.

First, there may be square brackets around the shorthand, and replacing [\d] (for example) would result in [[[:digit:]]], which ColdFusion doesn't care for. So, the square brackets must be removed first.

To do this, the javascript replace method works well, but it requires a regular expression for the match and a string for the replacement. Surround the expression with forward slashes so it will have a beginning and end. Escape special characters (including backslashes) with backslashes. In this instance, almost every character gets a backslash in front of it to build the match for this replacement (because almost every character in this combination is a special character). After the trailing backslash, provide a small "g" to make this a global match, working with every instance where the match occurs. Thus, in this example, [\d] becomes \d everywhere it occurs.

Suppose the user wants to match items that were NOT digits. In this case, the user might specify [^\d]. The code needs to convert this to ^\d.


// Change /^\d and ^\d and \d digit masks Workval=Workval.replace(/\[\\d\]/g,'\\d');
Workval=Workval.replace(/\[\^\\d\]/g,'\^\\d');

Now make mask-specific changes. This isn't a simple global replacment; because, there's a second problem: the up-caret (^) has a different meaning depending on its context, and the replacement syntax varies to match that meaning. If the up-caret occurs at the beginning of the pattern, it means that the pattern must begin to match from the beginning of the input. ^\d at the beginning of the line must become ^[[:digit:]]. The up-caret anywhere else it appears negates the meaning of the match, stating that the following character or group must NOT match the input. For example, ^\d somewhere in the middle or at the end of an expression says that this portion of the pattern must NOT be a character in the 0-9 range. Oddly, the formal way to match everything that is NOT a digit is [^[:digit]].

The next three lines do the work. The first line does the conversion for a digit match at the beginning of the input. The second line does the conversion for a NON-digit match anywhere. The third line does the simple conversion for a digit match anywhere in the input.


Workval=Workval.replace(/\/\^\\d/g,'^[[:digit:]]');
Workval=Workval.replace(/\^\\d/g,'[^[:digit:]]');
Workval=Workval.replace(/\\d/g,'[[:digit:]]');

The next conversion is almost like this one, only it's backwards! \D (note the letter's case) matches NON-digits. Thus, the logic seen above has to be reversed once the brackets are stripped off. First, strip the brackets as before. Then, handle ^\D at the beginning of the expression as the desire to match something other than a digit at the beginning of the input: ^[^[:digit:]]. Handle ^\D elsewhere as the desire to match a digit (!): [[:digit:]]. Handle \D without the up-caret as the desire to match something other than a digit: [^[:digit:]].


// Change /^\D and ^\D and \D NON-digit masks // (using :digit: backwards to achieve this result) Workval=Workval.replace(/\[\\D\]/g,'\\D');
Workval=Workval.replace(/\[\^\\D\]/g,'\^\\D');
Workval=Workval.replace(/\/\^\\D/g,'^[^[:digit:]]');
Workval=Workval.replace(/\^\\D/g,'[[:digit:]]');
Workval=Workval.replace(/\\D/g,'[^[:digit:]]');

Now that we're over the hump, repeat these two sets of code (match and NON-match) for other shorthand character representations. Note that "space" includes spaces and tabs; "alnum" (alphanumeric) includes letters and digits.


// Change /^\s and ^\s and \s space masks Workval=Workval.replace(/\[\\s\]/g,'\\s');
Workval=Workval.replace(/\[\^\\s\]/g,'\^\\s');
Workval=Workval.replace(/\/\^\\s/g,'^[[:space:]]');
Workval=Workval.replace(/\^\\s/g,'[^[:space:]]');
Workval=Workval.replace(/\\s/g,'[[:space:]]');

// Change /^\S and ^\S and \S NON-space masks // (using :space: backwards to achieve this result) Workval=Workval.replace(/\[\\S\]/g,'\\S');
Workval=Workval.replace(/\[\^\\S\]/g,'\^\\S');
Workval=Workval.replace(/\/\^\\S/g,'^[^[:space:]]');
Workval=Workval.replace(/\^\\S/g,'[[:space:]]');
Workval=Workval.replace(/\\S/g,'[^[:space:]]');

// Change /^\w and ^\w and \w alphanumeric masks Workval=Workval.replace(/\[\\w\]/g,'\\w');
Workval=Workval.replace(/\[\^\\w\]/g,'\^\\w');
Workval=Workval.replace(/\/\^\\w/g,'^[[:alnum:]]');
Workval=Workval.replace(/\^\\w/g,'[^[:alnum:]]');
Workval=Workval.replace(/\\w/g,'[[:alnum:]]');

// Change /^\W and ^\W and \W NON-alphanumeric masks // (using :alnum: backwards to achieve this result) Workval=Workval.replace(/\[\\W\]/g,'\\W');
Workval=Workval.replace(/\[\^\\W\]/g,'\^\\W');
Workval=Workval.replace(/\/\^\\W/g,'^[^[:alnum:]]');
Workval=Workval.replace(/\^\\W/g,'[[:alnum:]]');
Workval=Workval.replace(/\\W/g,'[^[:alnum:]]');

Mark a place for code to perform future conversions as you come across them.


// Future Project: replace \b word-boundary/backspace mask // and its complement: \B
// Make other changes here as you discover them...
Finally, ColdFusion doesn't want the leading and trailing slashes; so, remove them. Once you've done that, assign the result to a field that both you and ColdFusion can view and use. End the script.


// Last, remove leading and trailing slashes Workval=Workval.replace(/^[/]/,'');
Workval=Workval.replace(/[/]$/,'');

// Assign the result as the ColdFusion expression TheForm.CFExpression.value=Workval;
return true;
}
</script>

Build the Form

Still in Regex.cfm, build the form that will let you enter data, enter a javascript (or simple perl) regular expression, see the resulting ColdFusion expression, and see the result of applying that expression to the data.
Begin by setting defaults and opening the form. Have the form call the "demo" function (you just created) when the user submits the form. Use pre tags to simplify laying out this form.


<cfparam name="form.Data" default="">
<cfparam name="form.JSExpression" default="">
<cfparam name="form.CFExpression" default="">
<form name="Lab" method="post" onSubmit="demo(this)">
<pre>
<h4>Javascript and ColdFusion Regular Expression Laboratory</h4><strong>
<h5>Enter Data and a Javascript Regular Expression; Evaluate Using ColdFusion</h5>

In order to test the ability of regular expressions to detect control characters, use a textarea field instead of a text field for data input so it will accept such characters. Then provide fields to hold the javascript expression and ColdFusion expressions. For the value of each, use the appropriately named values previously defined. Define a submit button and provide instructions to the user.


Data: <cfoutput><textarea
name="Data" rows="2" cols="40">
#form.Data#</textarea></cfoutput>
Javascript: <input type="text" name="JSExpression" size="70"
value=<cfoutput>
"#form.JSExpression#"</cfoutput>>

ColdFusion: <input type="text" name="CFExpression" size="70"
value=<cfoutput>
"#form.CFExpression#"</cfoutput>>
Pressing {Submit} converts the javascript regular expression
to a ColdFusion regular expression and uses it to evaluate the data
<input type="submit" name="Go" value="Submit">

An expression that's mis-typed or can't be parsed by the combination of this code and ColdFusion can cause a ColdFusion error. Wrap the evaluation in try-catch tags. Clear a flag beforehand, and set the flag if the evaluation fails. If the failure flag is set, complain. Otherwise, report its value. Notice that its value might not be merely 0 or 1; it could be the position of a match (which in ColdFusion starts at one, not zero as in javascript). End the form.


<cfset Oops=0>
<cftry>
<cfset form.Result=reFind(form.CFExpression,form.Data)>
<cfcatch>
<cfset Oops=1>
</cfcatch>
</cftry>
<cfif Oops>
The ColdFusion expression is not valid
<cfelse>
<cfoutput>The ColdFusion expression returns #form.Result#</cfoutput>
</cfif>
</strong>
</pre>
</form>

Operation and Discussion

Browse Regex.cfm. Enter some expressions available to you in examples or in code you review. See how they're modified for use by ColdFusion and how they react to various data sets. Note that you can add leading and trailing slashes to the "ColdFusion" version and use it in javascript after you've tested it. This lets you use exactly the same expression in two environments: the client side and the server side.

Even expressions that don't completely meet your needs or correctly survive translation can be useful. You can paste the ColdFusion version back into the javascript field, modify it there, and press submit again to use the modified result in evaluation.

More likely, you've spotted a way to handle an exceptional condition and wish to implement it. By letting you enter shorthand expressions or letting you work with formal syntax (which could be initially generated from someone else's work in shorthand), this tool lets you use whichever syntax you're more comfortable with.

Yes, some operations are better handled using syntax specific to ColdFusion, and some can't be directly translated. Some are more easily handled using multiple expressions in succession than by a master expression that handles every nuance by itself. However, this laboratory will be fine for most of the expressions you write or discover and can save your time and your wits.


=Marty=




III. ColdFusion and File Maker
programmer@donomite.com



I have seen many times the question of how to link CF and FM together. I too
had this dilemma. Even tho File Maker 6 does have ODBC capabilities, it is
difficult, if not impossible, to make work. The best method with FM6 is to
make your ODBC connection and then suck all the data from the FM db into SQL
Server. That actually works fairly well, unless the data is corrupt in FM,
which is very common.

But what about if you want to have the ability to query the FM database and
just can not get the ODBC to work with Cold Fusion? It really isn't that difficult,
but it does take some work arounds that can get confusing. Do not try this
method unless you are a good to expert programmer in File Maker.

Here was my basic problem. I have a FM 3.0 application, so no ODBC. A very
old system to be sure. Outside sources want to connect to it, but the interface,
which is a series of DOS batch files combined with a small FM app, keeps failing.
Very complicated system involving 6 FM scripts and 4 DOS batch files on top
of a conversion application that moves data from an "IN" box to an "OUT" box.


First step of the process was to see how it flowed. The outside source would
FTP a request for data to the IN folder. The conversion app takes the data
and converts it into useable form called REQUEST.DAT, deletes the incoming request,
and saves this file to the OUT folder. The small FM app called INTERFACE.fm
is looping trying to import a record from the OUT folder. When a file shows
up, it imports it, stores the data, and deletes the file from the OUT folder.


Now INTERFACE calls the FIND function from the primary application, called PRIMARY.fm.
It goes and finds the data requested and then exports it to a file called OUTPUT.TAB
as a tab delimited file. INTERFACE creates the file name, exports a tab delimited
file that has the return FTP address, and the FTP info, then calls a batch file
that does the FTP.

Whew.

Here is what I did, and you can do this to pull data from File Maker into Cold
Fusion.

I did away with all the current batch files. What I did was let FM still import
the request and do the find and export the data. HOWEVER, what then happens
is that File Maker calls a batch file that calls the Cold Fusion template that
takes the data, cleans it up and FTPs it to the requester.

So the method for talking between CF and FM is to have the request for data
be imported into FM and FM do a find on the database and export the data in
a tab file. Then have CF "pick it up" and parse the data and do what you want
with it.

One key to remember. The INTERFACE must be on a seperate machine from the PRIMARY
db because INTERFACE is looping and whenever FM is locked in a loop like that,
NOTHING else can be run. By putting the endless loop on a seperate machine,
the File Maker Server does not lock up. The looping does not affect any other
applications being run. Just FM.

This is just a brief overview of what it takes to get data out of FM into CF.
If you would like to see the actual CF and FM scripts I used, email me at programmer@donomite.com




Publisher and Creator:
Nathan Stanford,
NathanS<at>nsnd.com
http://www.cftipsplus.com

Macromedia and ColdFusion are U.S. registered trademarks.


Copyright (c) 2000 - 2003
CFTIPSPLUS.COM and NSND.COM

Permission is granted to circulate this publication via
MANUAL forwarding by email to friends provided that the text is
forwarded in its entirety and no fee is charged.

Photo of Nathan Stanford
Nathan Stanford
LinkedIn

R. Marty Ladner's
Site