开发者

Determine if non-numerical characters have been pasted into UITextField

For a specialized calculator I would like to allow copy / paste for a textfield which is meant for numerical values only. So, only numerical c开发者_运维问答haracters should be actually pasted or the pasted string should be rejected if it contains non-numerical characters.

I was thinking about using UITextFieldDelegates textField:shouldChangeCharactersInRange:replacementString: method to check the pasted string for non-numerical characters. But NSString offers no method for checking whether it does NOT contain characters specified in a single set. So this way I would need to check occurances of characters from several sets, which is clumsy and these checks would run for every single number that would be typed in, which appears like quite some overhead to me.

Another way would be to iterate and check for every character in the replacement string whether there's a match in a numerical set.

Either way would propably work, but I feel like I'm missing something. Do you have any advice? Is there a convenience method to achieve this?


But NSString offers no method for checking whether it does NOT contain characters specified in a single set

sure it does.

if([myString rangeOfCharacterFromSet:myCharacterSet].location ==NSNotFound) 
{
//means there is no character from specified set in specified string
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜