IsValid and RegEx Issues

While writing some new documentation on ColdFusion MX 7 features you may have missed (for the soon to be released Fusion Authority Quarterly Update), I came across an interesting issue with the IsValid function.

This function is used to test if a ColdFusion variable is of a particular data type or if it is a particular value (email address, credit card, etc). In many ways this is a single function that can stand in for most of the standard "IS" functions (IsStruct, IsQuery, etc.) One noted exception is that the function has no existance checking (IsDefined).

One feature of the function is the ability to use a regular expression as a validator. This makes the IsValid tag a perfect replacement for using REFind to do regular expression pattern validation. That is, with a single exception. REFind is case sensitive and has a NoCase version to deal with non-case sensitive matches. IsValid is only case sensitive.

For many, this may mean that using IsValid in place of REFind for regular expression pattern validation is not an option. To those I say, there is a simple solution.

ColdFusion MX 7 has some advanced options for regular expressions which include a simple prefix for any expression to make it case insensitive. To make IsValid in regular expression mode case insensitive, just do the following:

IsValid('regex', variable, '(?i)pattern')

The (?i) syntax at the start of a regular expression pattern makes the entire match case insensitive. Other advanced regular expression prefixes can be used with the function as well, which can greatly expand its capabilities.

Note that we are talking about the ColdFusion-style regular expressions here. The documentation for the IsValid function incorrectly states that it uses Javascript-style regular expression syntax. This is not the case.

There are a few other quirks with the function that you're almost never going to run into and I suggest you keep an eye on the IsValid LiveDocs entry for any changes and clarifications.

Comments
Ben Nadel's Gravatar I have not used the IsValid method very much yet, but I have to wonder about the benefits of using it over older, more specialized methods such as REFind() and IsStruct(). I can understand if the IsValid() function is much more powerful/efficient, but my gut reaction is that IsValid has to run some sort of switch statement on the type of expression being validated (regexp, SSN, phone, etc) and Then run the validation itself. Why not skip the type-checking and just call the actual checking method (ex. REFind())?

Again, I have not used it very much, so I am just giving stream-of-conscience here. I can understand that it can be great for people who might not know regexp for SSN matching and URL mathing, and that's really really cool... but is it really worth moving over to it for simple things like IsArray and IsStruct?
# Posted By Ben Nadel | 6/19/06 11:33 AM
Michael Dinowitz's Gravatar Logic says that there is some sort of switch inside that converts the text 'validation type' into a call to the proper type. This would mean that IsStruct and IsValid for structures are using the same code under the hood but the isValid has 'more' overhead. This would suggest that the more specialized versions are better.

I think the point of the function is as you said, to let people get string validation without having to write RegEx. Personally, I'd rather write some tight RegEx. :)
# Posted By Michael Dinowitz | 6/19/06 11:59 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner
House of Fusion | ColdFusion Jobs @ House of Fusion | Fusion Authority