Search code examples
regexcoldfusion

How to disallow non-alphanumeric characters in ColdFusion using a RegEx


I am using ColdFusion 9.0.1.

I am trying to test whether a user has provided a non alphanumeric value. If they have, I want to return false. I'm pretty sure I am close, but I keep getting an error:

Complex object types cannot be converted to simple values.

I've tried multiple ways of making this work, but I can't get it to work.

Specifically, I want to allow only a through z and 0 through 9. No spaces, or special characters.

Can you help me tweak this?

    <cfscript>
        LOCAL.Description = trim(left(ARGUMENTS.Description, 15));
        if (len(LOCAL.Description) lte 4) {
            return false;
        } else if (reMatchNoCase("[^A-Za-z0-9_]", LOCAL.Description) neq "") {
            return false;
        } else {
            return true;
    </cfscript>

W


Solution

  • reMatchNoCase returns Array which cannot be compared to the string, use ArrayLen() on the result in order to find out if there any matches

    There is actually another problem in your code. First line will produce an error if the length of the description is less than 15, which means that the first IF is obsolete since it will always be false.