Allow specific characters on GUI textField?

Hello,

I am looking on a way to prevent user from typing letters or any other special character (%^&@!$) to a GUI text field. Only numbers.

I tried a lot of ways I read on forums but NONE of them work.
Any help?

function OnGUI () {
	value1_string = GUI.TextField (Rect (10, 10, 200, 20), value1_string, 25);
	value1 = int.Parse(value1_string);
	
	value2_string = GUI.TextField (Rect (10, 30, 200, 20), value2_string, 25);
	value2 = int.Parse(value2_string);
}

Here is a way to do it. I’m not sure if it is the best way:

#pragma strict

var value1_string : String;
var value1 : int;

    function OnGUI () {
    	var e = Event.current;
    	if ((e.type == EventType.KeyDown) && !char.IsDigit(e.character)) 
    		e.type = EventType.Used;
    		
    	value1_string = GUI.TextField (Rect (10, 10, 200, 20), value1_string, 25);
    	int.TryParse(value1_string, value1);
    }

I’ve gone to int.TryParse() which does not generate an exception when it fails. Currently this code is generating multiple calls to TryParse() each frame…even when the string is empty. I don’t know what your conditions are for processing the number.