Limiting keyboard to numbers only

hey everyone,

how would i go about limiting the iphonekeyboard to only allow numbers to be entered into the gui text?

this is the script im using to get the keyboard up

var inputText : String = "text";
private var keyboard : iPhoneKeyboard;
// Updates button's text while user is typing
function OnGUI() {
    if (GUI.Button(Rect(0, 10, 200, 32), inputText))
        keyboard = iPhoneKeyboard.Open(inputText);
    
    if (keyboard)
        inputText = keyboard.text;
}

its straight from the reference for now

would i have to make my own keyboard?

pat_sommer,

The iPhoneKeyboard functionality itself is a bit limited. My suggestion would be to simply check the text value each frame and parse out all non-numeric characters. You could easily do this with something similar to the following code:

myStringBuilder.Length = 0;
for(int i = 0; i < inputText.Length; i++)
{
    if (char.IsNumber(inputText[i]))
        myStringBuilder.Append(inputText[i]);
}

If you are worried about performance you could use this as a validation when the user finishes, then prompt them if they enter an invalid character. I doubt you will have too much going on while the user is entering this information so I would not worry about performance unless it turns out to be an issue.

You can change which keyboard appears by using the iPhoneKeyboardType enumeration. If you set it to iPhoneKeyboardType.NumberPad you will get a keypad that is designed for number entry.

var inputText : String = "text";
private var keyboard : iPhoneKeyboard;
// Updates button's text while user is typing
function OnGUI() {
    if (GUI.Button(Rect(0, 10, 200, 32), inputText))
        keyboard = iPhoneKeyboard.Open(inputText, iPhoneKeyboardType.NumberPad);
    
    if (keyboard)
        inputText = keyboard.text;
}

http://unity3d.com/support/documentation/ScriptReference/iPhoneKeyboardType.html

i tried that but it seems to just forward me to where the numbers are, but not actually be a “number pad” so the user can still type in a non number character

You sure? This is the number pad I get when I use iPhoneKeyboardType.NumberPad.

maybe cuz this is ipad and not an iphone?

Is there anyway to get rid of the done button and the input box to leave just the number pad? I’ve seen it this way on other apps. It seems daft if you press on a editbox and then this pad appears and duplicates the input box.

You can with some keyboards using TouchScreenKeyboard.hideInput, but not the number pad.

file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/TouchScreenKeyboard-hideInput.html

Thanks but how did they do this then?

Sorry if I didn’t make it clear, I was referring to the Unity command not working with the number pad. The screenshot probably isn’t from a Unity app.

Can you not set inputType with the new TouchScreenKeyboard?? I’m surprised how limiting it is. I just want to bring up the iOS numeric pad…is this not possible?