2

Just ran into a rather strange bit of functionality with the baked in IsNumeric function is classic ASP.

I had a JSONArray class that prints out its contents to a string - it was using IsNumeric to determine whether or not to escape values with "'s. Everything was working fine until today a client reported a previously unseen error!

After some digging I found that a handful of records in the JSON array that was being returned from an AJAX call were poorly formed. More digging. Turns out strings that were formatted similar to '2D25' would be evaluated as numeric by IsNumeric and therefore not be escaped!

Does anyone know why this is? Does ASP think the D is interchangeable with a .?

Also, how should I go about fixing this? Would a regular expression be a better solution?

Thanks!

FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55

2 Answers2

4

2D25 is a hex value. See http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.information.isnumeric.aspx, in particular: "IsNumeric returns True if Expression is a string that contains a valid hexadecimal or octal number." RegEx is probably your best bet.

<<< Edit :: Classic ASP Reference >>>

http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html

In Classic ASP, the D is used to signal double precision.

James L.
  • 9,384
  • 5
  • 38
  • 77
  • I figured it was something like this, however the documentation you provided is for .Net, not applicable for classic asp. – FlyingStreudel Jul 17 '12 at 17:36
  • Ah -- you are right. The 'D' in classic ASP was once used to signal 'double' precision. It sounds like that is what you are seeing. I'll add another link for Classic ASP to the answer as a reference... You have to scroll about 1/2 way down the page to the VBScript section. – James L. Jul 17 '12 at 20:07
2

RegEx is indeed the way to go, here an example of such a sollution

Function myIsNumeric(ByVal Value)
  Set regEx = New RegExp
  regEx.pattern = "^(0|[1-9][0-9]*)$"
  myIsNumeric = Regex.Test(Value)
End Function

here a test

if myIsNumeric("2D25") then
  Wscript.Echo "true"
else
  Wscript.Echo "false"
end if

gives false

peter
  • 41,770
  • 5
  • 64
  • 108
  • RegEx is a reasonable solution however when testing over an array as in the question, a function that returns a `RegExp` would be preferable. The loop over the array needs only call `.Test` repeatedly against the same instance of `RegExp`. – AnthonyWJones Jul 17 '12 at 20:38
  • true but readability is also important and my expereince is that in some area's vbscript is such a snail that these little differences won't matter – peter Jul 17 '12 at 20:44
  • I don't think my suggestion would do much damage to readability and it would depend on just how many entries are in the array. I wonder how long it takes to instance a RegExp object and parse an expression say 1,000 times? – AnthonyWJones Jul 17 '12 at 21:03
  • @ AnthonyWJones, please post your version, we are here to learn isn't it and i have seen some of your answers which learned me a lot although i program in vbscript for 10 years now (but coming to an end now i'm in love with Ruby), if i feel into i will benchmark the two approaches – peter Jul 17 '12 at 21:16