Allow only numbers in an input field

I want to know how to restrict the input field numbers and just ignore the letters, I found it but the method of “int” does not exist, or rather “int” does not exist. I Program in C #

GUI.Label(new Rect(positionX, positionY, _W, _H), "Epaisseur (mm):");
				stringToEditH = GUI.TextField (new Rect (positionX, positionY+_H, _W/3f, 20f), num.ToString(), 3);
				
			    int temp = 0;
			    if (int.TryParse(stringToEditH, temp))
			    {
			        num = Mathf.Clamp(0, temp);
			    }
			    else if (text == "") num = 0;

It's very easy if you want to use a regex:

I don't usually use C#, but a quick look at the documentation makes me think it would look like this:

/* define a pattern: everything but 0-9 */

Regex rgx = new Regex("[^0-9]");

/* replace all non-numeric characters with an empty string */

stringToEditH = rgx.Replace(stringToEditH, "");

 

You might have to fiddle with it, but that's the idea....

------------

If you don't want to use a regex, you could do it yourself like this:

function stripNonNumerics ( inputString : String ) : String
{
    var theResult : String = "";

    for ( var i = 0; i < inputString.length; i ++ ) {

        var theString : String = inputString.Substring(i, 1);
        var tempInt   : int;

        if ( int.TryParse(theString, tempInt) ) {
            theResult += theString;
        }
    }

    return theResult;
}

 

There are probably a few better ways to do it, but that seems to work for me.

That's in UnityScript... it will probably be easier in C# where you can use chars!!