Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

check if the input is String and not numeric

Discussion in 'Scripting' started by chemsoun, Apr 11, 2015.

  1. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    hi
    for my unity game i'm creating a register menu, what i want is to check if the inputs are string type and not containing numeric values or special caracters
     
  2. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    Regular Expressions seem to be what you are looking for; at least the way I interpret your question. The syntax might be a bit unintuitve, but that is something where internet can help. Here is a small example:
    Code (CSharp):
    1. public bool IsValidName(string input)
    2. {
    3.     return System.Text.RegularExpressions.Regex.IsMatch(input, "[^A-Za-z]");
    4. }
    The 'pattern' says the following:
    1. "[^A-Za-z]": Any one character between the square-brackets.
    2. "[^A-Za-z]": Make that any character, except all others between the brackets (its position is important).
    3. "[^A-Za-z]": Character in the range of 'A' up to 'Z'. The hyphen is an indication for this.
    Now, if the inputted string contains a character that is not a letter from A to z (either upper- or lowercase), the inputted string is 'illegal'. If you need some more characters, you should add them as you see fit, note though that there are a few special characters. Here is the relevant page.
     
    AnaLuyza and chemsoun like this.
  3. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    Thanks Mr. Mud, i found the solution in

    Code (CSharp):
    1. if (!name.All(char.IsLetter))
    that works fine for me, your solution is very interesting