QuackFuzed.com is the personal ColdFusion coding blog of Matt Quackenbush. It exists primarily as a place for the author to learn, and hopefully to assist others in learning and/or avoiding some of the same pitfalls and mistakes. (Quack certainly makes enough mistakes daily to make up for the entire ColdFusion community.)
isValid() Bug?
Posted on July 15, 2008 at 6:22 AM in ColdFusion
I wrote some brand new validation routines over the weekend and went to test them out tonight; lo and behold, they completely failed. Pass valid data, and they simply failed. I was scratching my head. I've written a ton of regex (regular expressions) over the years, so I knew that my regex was correct. It had to be something else.
Latest Articles
- cfUniForm - Custom Validation Demo
- NOTE: cfUniForm Does Not Require ValidateThis!
- cfUniForm v2.6 - Global Plugin Configs + ValidateThis! ...
- Using Multi-Word-Actions in ColdBox 2.6
- Transfer: Many-to-One or One-to-One?
Eventually something really brilliant and witty will appear right here.
Categories
- ColdBox (20) [RSS]
- ColdFusion (59) [RSS]
- Fusebox (3) [RSS]
- General (10) [RSS]
- jQuery (9) [RSS]
- Kalendar (1) [RSS]
- Rants (3) [RSS]
- Transfer (6) [RSS]
- Uni-Form Tag Library (17) [RSS]


On 7/15/08 at 9:22 AM, Shaun Moore said:
On 7/15/08 at 9:25 AM, Shaun Moore said:
On 7/15/08 at 11:49 AM, Brad Wood said:
You would probably need to add that there could be zero or more non-uppercase characters before and after the one or more upper case charactes. Something like"
.*[A-Z]+.*
Right now you are telling isvalid that the ENTIRE string needs to consist of one or more uppercase characters.
On 7/15/08 at 2:24 PM, Matt Quackenbush said:
On 7/15/08 at 2:38 PM, Shaun Moore said:
A great way to test for this quickly is using this site: http://regexpal.com/ which has a regex tester. You can place the regex in the top and place your string in the bottom and see if it will match. Using your regex it will match PASSWORD but not the string you have.
On 7/15/08 at 2:53 PM, Matt Quackenbush said:
On 7/15/08 at 3:01 PM, Shaun Moore said:
Still, I don’t see this as a bug, this is just how the IsValid function is supposed to work. I use it a lot to check form data to ensure that it is the correct type.
On 7/15/08 at 3:10 PM, Matt Quackenbush said:
1. The documentation simply does not state that.
2. That behavior is the stupidest thing on the planet. WTF is the point of writing regex if the function is going to override what you write? And forcing the entire string to match from start to finish when the regex only calls for a one (or more) characters is absolutely asinine.
On 7/15/08 at 3:21 PM, Shaun Moore said:
IsValid("regex", Form.IMGCode, "[a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9]{2}")
I've used this in certain date fields as well to be sure they are using a date range I want in a format I want:
IsValid("regex", Evaluate("Form.#Variables.FN#"), "^20[0-9]{2}")
So for cases like this it is very useful.
On 7/15/08 at 3:34 PM, Matt Quackenbush said:
if ( reFind("\A[a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9]{2}\Z", form.IMGCode) EQ 1 ) { return true }
and
if ( reFind("\A^20[0-9]{2}\Z", form[variables.FN]) EQ 1 ) { return true }
Relying on what amounts to bullshit behavior from isValid() will cause you problems if you move that to any other language, or even to reFind()/reFindNoCase(). For example, try this at the JavaScript site you linked to...
regex: [a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9]{2}
string: 8888a99A99zzzzzzzzzzzzzzzzzzzzzzzzzzzz
You'll correctly get a match. Now then, try the following...
regex: ^[a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9]{2}$
string: 8888a99A99zzzzzzzzzzzzzzzzzzzzzzzzzzzz
You will *not* get a match on that one, because the regex indicates that it must be the full string.
On 7/15/08 at 3:36 PM, Brad Wood said:
The Livedocs for refind say:
"This function finds the first occurrence of a regular expression in a string."
"occurrence ... in a string" means that it looks for your expression among other, possibly non-matching strings inside of the string you specify.
The Livedocs for isvalid say:
"A JavaScript regular expression that the parameter must match"
Notice it says "the parameter", not "a portion" or "part" of the parameter. The entire parameter must be described by the pattern.
refind is meant for searching within a string, isvalid is meant for validating a string in its entirety. I think the behavior makes sense.
On 7/15/08 at 3:47 PM, Matt Quackenbush said:
As far as the reFind()/reFindNoCase() portion of your comment goes, it seems that you are suggesting that they should not be used for matching an entire string. If that's the case, I do not agree.
On 7/15/08 at 4:08 PM, Matt Quackenbush said:
Yes, isValid() changes the behavior of what I wrote, which is very aggravating, because I believe the regex author should have full control over the regex. Otherwise, why write it? That said, if the docs _clearly_ stated that the parameter would have to match from beginning to end, then I could accept that. At least then the author would know that they have to change their regex.
Thank you Shaun and Brad both for your input. :-)
On 7/15/08 at 4:14 PM, Brad Wood said:
I can see your confusion since isvalid basically does the same thing as ^[A-Z]+$ when all you typed in was [A-Z]+ but the context of the function and what the docs describe as its behavior determine exactly what it is going to do with your regular expression.
I wouldn’t say the function is “changing” your regex. Your regex is simply a pattern, and the function can look for the pattern however and wherever it pleases based on its requirements.
On 7/15/08 at 4:33 PM, Matt Quackenbush said: