我的应用程序允许用户在UITextField
控件中输入数字值(货币),但不幸的是,我希望可用的键盘布局不是内置选项之一,因此我必须在界面生成器中选择“数字和标点符号”选项。下面是IB中的相应对话框窗口:
因此,当我的应用程序要求用户输入时,它将显示以下内容:
这是非常好的,但是看看用户可以使用的所有额外密钥!可以很容易地输入"12;56!“在文本字段中,我假设我必须以某种方式验证它。
所以我的问题是:如何验证输入到UITextField
中的货币值
发布于 2011-01-31 20:40:41
我有回答的冲动,因为这是我在谷歌上看到的第一个条目,排名最高的答案不允许我输入货币。
我是德国人,在德国(以及其他许多国家),我们使用,
作为小数分隔符。
我刚刚写了一个类似的方法,这就是我现在所拥有的。
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
static NSString *numbers = @"0123456789";
static NSString *numbersPeriod = @"01234567890.";
static NSString *numbersComma = @"0123456789,";
//NSLog(@"%d %d %@", range.location, range.length, string);
if (range.length > 0 && [string length] == 0) {
// enable delete
return YES;
}
NSString *symbol = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
if (range.location == 0 && [string isEqualToString:symbol]) {
// decimalseparator should not be first
return NO;
}
NSCharacterSet *characterSet;
NSRange separatorRange = [textField.text rangeOfString:symbol];
if (separatorRange.location == NSNotFound) {
if ([symbol isEqualToString:@"."]) {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersPeriod] invertedSet];
}
else {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersComma] invertedSet];
}
}
else {
// allow 2 characters after the decimal separator
if (range.location > (separatorRange.location + 2)) {
return NO;
}
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbers] invertedSet];
}
return ([[string stringByTrimmingCharactersInSet:characterSet] length] > 0);
}
发布于 2009-06-19 14:20:47
虽然你可以在NSNumberFormatter中闲逛,但我发现筛选掉0-9和之外的任何东西都更容易。
这对我来说效果很好:
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
发布于 2012-09-06 12:50:14
我发现这是一种相对干净的方法。我没有用非美国货币测试过它,但是因为它使用了NSNumberFormatter的属性,所以我相信它应该能正确地处理它们。
首先在某个地方设置一个格式化程序:
formatter = [NSNumberFormatter new];
[formatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[formatter setLenient:YES];
[formatter setGeneratesDecimalNumbers:YES];
使用格式化程序解析和重新格式化它们的输入。当数字被添加和删除时,它还处理数字的移位。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *replaced = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSDecimalNumber *amount = (NSDecimalNumber*) [formatter numberFromString:replaced];
if (amount == nil) {
// Something screwed up the parsing. Probably an alpha character.
return NO;
}
// If the field is empty (the initial case) the number should be shifted to
// start in the right most decimal place.
short powerOf10 = 0;
if ([textField.text isEqualToString:@""]) {
powerOf10 = -formatter.maximumFractionDigits;
}
// If the edit point is to the right of the decimal point we need to do
// some shifting.
else if (range.location + formatter.maximumFractionDigits >= textField.text.length) {
// If there's a range of text selected, it'll delete part of the number
// so shift it back to the right.
if (range.length) {
powerOf10 = -range.length;
}
// Otherwise they're adding this many characters so shift left.
else {
powerOf10 = [string length];
}
}
amount = [amount decimalNumberByMultiplyingByPowerOf10:powerOf10];
// Replace the value and then cancel this change.
textField.text = [formatter stringFromNumber:amount];
return NO;
}
https://stackoverflow.com/questions/342181
复制相似问题