ColdFusion 10: Is "5a" numeric?

I was sad to find that two bugs I logged for Adobe ColdFusion haven’t been resolved in the 10 Beta release. What’s more they’re been marked closed and deferred due to “NotEnoughTime”, but I guess Adobe must be pretty busy as I didn’t think these would be that involved to fix.

When is a valid numeric, not a number?

Answer: When it’s a date / time string. Currently IsValid(“Numeric”) doesn’t behave like IsNumeric() and accepts date / time string as valid. In the following example IsNumeric will return false for all the values but IsValid(“Numeric”) returns true instead.

<cfscript>
  values = ['1a','2p','3am','4pm','23pm','01:13:24','23:45:12am',
           '01/01/2020','2010-09-08 07:06:05'];
  iEnd = ArrayLen(values);
  for (i = 1; i <= iEnd; i++) {
    WriteOutput('IsNumeric("' & values[i] & '") = ' & IsNumeric(values[i]) & '<br />');
    WriteOutput('IsValid("numeric", "' & values[i] & '") = ' & IsValid('numeric', values[i]) & '<br />');
  }
</cfscript>

You’ll find this registered with the Adobe Bugbase with ID 3042477.

Is this the right room for an argument?

Only if you want to argue over an argument type of “Numeric” accepting date / time strings. Yup, just like the previous bug but this time it’s the “type” attribute for functions / component methods.

<cfscript>
  string function NumericOnlyPlease(required numeric num) {
    return "Accepted the value: " & arguments.num & " as an argument type of 'Numeric'.";
  }
  values = ['1a','2p','3am','4pm','23pm','01:13:24','23:45:12am',
           '01/01/2020','2010-09-08 07:06:05', 'zzz'];
  iEnd = ArrayLen(values);
  for (i = 1; i <= iEnd; i++) {
    try {
      WriteOutput('numericOnlyPlease("' & values[i] & '") = ' & numericOnlyPlease(values[i]) & '<br>');
    } catch(any e) {
      WriteOutput('numericOnlyPlease("' & values[i] & '") = <strong>ERROR</strong>: #HtmlEditFormat(e.message)#' & '<br>');
    }
  }
</cfscript>

With the above code, all the values in the array will be accepted apart from the last one which correctly throws an error since “zzz” is obviously not numeric. Again this bug has been logged with Adobe under ID 3042477.


Comments


David Boyer
David Boyer

Full-stack web developer from Cardiff, Wales. With a love for JavaScript, especially from within Node.js.

More...