0

I am unable to find a Unicode supported isNumeric() function for javaScript and VBScript. The functions are there, but are not unicode supported.

Even if there are no Unicode suported versions, then are there any API in both languages supporting isNumeric() or isDigit() functionality?

Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
Zaki Imtiaz
  • 183
  • 1
  • 2
  • 16
  • Can you give an example of what fails? – Alex K. Sep 13 '11 at 11:57
  • Javascript behaves quite well with different languages/weird characters, so maybe you just need a good implementation of isNumeric function, like the one here http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/174921#174921 – WTK Sep 13 '11 at 12:14
  • what about use [isNaN()](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN) ? – Jean-Charles Sep 13 '11 at 12:27

1 Answers1

2

I have come upon the same problem a couple of weeks ago, and the solution I used was to check my character code against the unicode char code ranges that may define a digit.

Here they are (the ranges come from PERL sources, as they already did the work!!):

var isDigit = function(ch)
{
  var iCode = ch.charCodeAt(0)
  return ( 0x0030 <= iCode && iCode <= 0x0039 )
      || ( 0x0660 <= iCode && iCode <= 0x0669 )
      || ( 0x06F0 <= iCode && iCode <= 0x06F9 )
      || ( 0x0966 <= iCode && iCode <= 0x096F )
      || ( 0x09E6 <= iCode && iCode <= 0x09EF )
      || ( 0x0A66 <= iCode && iCode <= 0x0A6F )
      || ( 0x0AE6 <= iCode && iCode <= 0x0AEF )
      || ( 0x0B66 <= iCode && iCode <= 0x0B6F )
      || ( 0x0BE6 <= iCode && iCode <= 0x0BEF )
      || ( 0x0C66 <= iCode && iCode <= 0x0C6F )
      || ( 0x0CE6 <= iCode && iCode <= 0x0CEF )
      || ( 0x0D66 <= iCode && iCode <= 0x0D6F )
      || ( 0x0E50 <= iCode && iCode <= 0x0E59 )
      || ( 0x0ED0 <= iCode && iCode <= 0x0ED9 )
      || ( 0x0F20 <= iCode && iCode <= 0x0F29 )
      || ( 0x1040 <= iCode && iCode <= 0x1049 )
      || ( 0x17E0 <= iCode && iCode <= 0x17E9 )
      || ( 0x1810 <= iCode && iCode <= 0x1819 )
      || ( 0x1946 <= iCode && iCode <= 0x194F )
      || ( 0x19D0 <= iCode && iCode <= 0x19D9 )
      || ( 0xFF10 <= iCode && iCode <= 0xFF19 )
      || ( 0x104A0 <= iCode && iCode <= 0x104A9 )
      || ( 0x1D7CE <= iCode && iCode <= 0x1D7FF )
}

By example, in Tamil, the character code for "one" is 0x0BE7, cf. http://www.fileformat.info/info/unicode/char/BE7/index.htm, and you can see it is part of the range 0x0BE6 => 0x0BEF so it's a-ok.

Shautieh
  • 746
  • 10
  • 17
  • That is a very long procedure. What I get is, if I need isNumeric() function for 300 languages, I have to check all the code ranges...urgh......that would be hectic dude! :( – Zaki Imtiaz Sep 13 '11 at 12:45
  • Those are supposed to be *all* the digit ranges defined in unicode. Every language doesn't have its own digit range! Edit: if you think there a built-in isNumeric function or equivalent in javascript that will do this, I fear you are wrong... And *no* code using casting to ints like "(input - 0) == input" will work. – Shautieh Sep 13 '11 at 12:55
  • O yes, you probably are right about. There are couple of numeral codes that are needed to be checked for...Thanks man. That might solve my problem. – Zaki Imtiaz Sep 13 '11 at 13:43