How to Get a Textfield to Accept only numbers and periods.

Currently I’m using this section of code:

if (chr < '0' || chr > '9' && chr != '.') {
   Event.current.character = '\0';
}

However, I’ve come to realize that I can’t place a period into the text field with or without this code. I can’t use commas either. Long story short I’m getting frustrated not being able to place/display float values in these fields.

Something like this: http://answers.unity3d.com/questions/40103/text-field--inputting-numbers-only.html

It’s just a matter of fixing the logic by using parentheses, since the expression is evaluated left to right, and elements inside parentheses are grouped:

if ( (chr < '0' || chr > '9') && chr != '.') {
     Event.current.character = '\0';
}

However that would still allow multiple periods, which is probably not desirable, so that could be fixed (assuming that your string variable is called “myString”):

if ( ((chr < '0' || chr > '9') && chr != '.') || (chr == '.' && myString.Contains(".")) ) {
    Event.current.character = '\0';
}