Scott Hanselman

Internationalization/I18n: Char.IsDigit() matches more than just "0" through "9"

March 10, 2004 Comment on this post [5] Posted in ASP.NET | Javascript | Bugs
Sponsored By

Raymond Chen just gave me a "duh" moment, by pointing out the obvious-only-if-you-think-about-it.  Char.IsDigit() doesn't mean 'IsZeroToNineInEnglish', it means 'is in the decimal range of 0 to 9' and darnnit if there aren't other ways (other than 0,1,2,3,4,5,6,7,8,9) to express them! :)

So let's run an experiment.

class Program {
  public static void Main(string[] args) {
    System.Console.WriteLine(
      System.Text.RegularExpressions.Regex.Match(
        "\x0661\x0662\x0663", // "١٢٣"
        "^\\d+$").Success);
    System.Console.WriteLine(
      System.Char.IsDigit('\x0661'));
  }
}

The characters in the string are Arabic digits, but they are still digits, as evidenced by the program output:

True
True
Uh-oh. Do you have this bug in your parameter validation? (More examples..)
If you use a pattern like @"^\d$" to validate that you receive only digits, and then later use System.Int32.Parse() to parse it, then I can hand you some Arabic digits and sit back and watch the fireworks. The Arabic digits will pass your validation expression, but when you get around to using it, boom, you throw a System.FormatException and die.
[Raymond Chen]

Arabic speakers (مرحبًا, كيف حالك ؟ and forgive me, it's been college since I studied Arabic) how to you handle numeric validation in JavaScript AND guarantee that the JavaScript you use on the client-side is semantically equivalent to the server-side code?

Either way, my friends, read, grok, and be enlightened.  Muy interesante.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 10, 2004 15:22
Smart Ass Comment of the Day:

If we all spoke english and used ASCII, we wouldn't have this problem.

Feet
Inches

$0.02

Tower of Babel
March 10, 2004 17:05
Feature Request:

Would there be any remote possibility of adding a feature that would allow posters to delete their own stupid comments? Or edit them? Sort sort of method for self-moderation...

Just a thought...


March 10, 2004 19:14
You may thow up on this, but I would use Web Service Behavior if I really want to guarantee that the JavaScript I use on the client-side is semantically equivalent. You ARE technically using server side logic and it does talk to the server via SOAP behind the scenes, but there were times when it worked perfectly for me.

Yes, you have to also guarantee that all the users use none other than IE. :-)
March 11, 2004 1:48
مرحبا سكوت , كيف حالك انا اتابعك دائما
March 11, 2004 1:51
Hi scott

How are you , I read your blog , every time

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.