开发者

how to validate textfield that allows only 0,1,..9 and . symbol

i place 4 textfields in my application.

I need that 4 textfields allows only开发者_如何学Python numbers 0,1...9 and . symbol.

For that i place keyboard numbers&punctuation. it is ok but In my keypad allows ABC,-,/,(,that all are activated.

But in my textfield, that allows only 0,1,...9 and . symbol.

How can i done this.

can any one pls help me.

Thank u in advance.


If you are targeting iOS 4.1 and later, you can use UIKeyboardTypeDecimalPad. If you need to support older versions, require a decimal point, and don't want anything else, you will have to create a custom keyboard.

If you want to filter out the other keys without removing them, assign some object as the text field's delegate. That object should contain the following code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    static NSCharacterSet *charSet = nil;
    if(!charSet) {
        charSet = [[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet] retain];
    }
    NSRange location = [string rangeOfCharacterFromSet:charSet];
    return (location.location == NSNotFound);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜