Search Unity

check user input

Discussion in 'Scripting' started by VeganApps, Jan 5, 2009.

  1. VeganApps

    VeganApps

    Joined:
    Jun 30, 2006
    Posts:
    263
    Hello,

    1) I am looking for a method to check if a number (float) is written with "," or ".".
    Like "21,00" or "17.01". And correct the input..

    2) And I am searching a method to e.g. check if the user filled only numbers into a textfield..
    Like "20" (correct) and "20ABC" (not correct)

    Does mono offer something like that for the Unity webplayer ?

    Greetings
    DH
     
  2. LDiederich

    LDiederich

    Joined:
    Jan 5, 2009
    Posts:
    15
    Hi DynamicHead,

    1. the easiest thing to do that is to convert the "," to "." and check the ammount of "." in your Textbox or you use Regular Expressions.
    2. you can use TryParse to check the user input. Its a c# sample it would be run on Mono too. But you must change it with Input.GetAnyKey or something like that to catch the Event.

    Code (csharp):
    1. private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
    2. {
    3.      int isNumber = 0;
    4.      e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
    5. }

    I'm at the office atm, no Mac or Unity here :([/quote]
     
  3. VeganApps

    VeganApps

    Joined:
    Jun 30, 2006
    Posts:
    263
    Thank you.

    1) Sounds good. Any idea how I can find out if the used computer uses the number format "," or "." ? Because this varies from language to language..
     
  4. LDiederich

    LDiederich

    Joined:
    Jan 5, 2009
    Posts:
    15
    Hi,

    i dont know if it work with unity but you can use System.Globalisation in .net and mono. Link
    If you replace always the "," with "." you doesn't need the culture informations.

    Lars
     
  5. VeganApps

    VeganApps

    Joined:
    Jun 30, 2006
    Posts:
    263
    Thanks you. I'll give it a try.