可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
First, I setup up the keyboard for the UITextField to use the number with decimal style. So the user can only enter numbers and a single decimal.
What I want to do is test the input as the user enters it and prevent multiple decimals from being entered and limit the decimal portion of the number to two places. I do not want to round off the number nor even treat the input as a number. I simply want to prevent the user from entering more then two digits to the right of the decimal place.
回答1:
The solution ultimately turned out to be fairly trivial. Unfortunately a lot of the questions and answers related to this question are about validating or formatting numeric values, not controlling what a user could input.
The following implementation of the shouldChangeCharactersInRange delegate method is my solution. As always, regular expressions rock in this situation. RegExLib.com is an excellent source for useful RegEx samples or solutions. I'm not a RegEx guru and always struggle a bit putting them together so any suggestions to improve it are welcome.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.quantityTextField)
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];
if (numberOfMatches == 0)
return NO;
}
return YES;
}
The above code allows the user to input these kinds of values: 1, 1.1, 1.11, .1, .11. Notice that this implementation does not replace the text field's string, which would causes a recursion. This solution simply rejects the next character the user inputs if the 'newString' does not match the regex.
回答2:
Good! If someone need it working with non-US currencies: euro or other ones with fraction separated by comma instead of dot use this one:
NSString *expression = @"^([0-9]+)?([\\,\\.]([0-9]{1,2})?)?$";
The only difference is that it allows comma or dot. ([\,\.] - either . or ,) Only trick is that you need to replace comma with dot when using your acquired number, because computer uses dot to separate fraction, not the comma.
回答3:
The standard way of dealing with this issue in iOS is attaching a UITextFieldDelegate
to your UITextField
, and implement textField:shouldChangeCharactersInRange:replacementString:
method. Inside this method you can validate the string to be of the correct "shape" for your purposes (one dot, no more than two digits after the dot, etc.) and supply a different string if the input does not follow the expected format.
回答4:
After much experimentation and looking at other's solutions (which seem to be overly complex and fiddly) I came up with the following, which prevents the user from entering anything but a valid currency amount. Basically, let an instance of NSNumberFormatter (created elsewhere in the viewController) do the hard work:
- 1) convert the text to a number (if nil, then there are invalid characters, so return NO)
- 2) then convert the number to currency
- 3) then convert the currency string back to number and, if different to the original (means too many decimals entered), return NO
- 4) then make an NSDecimalNumber, which is what I wanted. May be different for you, of course.
Works for me so far - hope that this helps someone.
Firstly, I set the keyboard to UIKeyboardTypeDecimalPad. Then I used the following code
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
NSString *textString = [textField.text stringByReplacingCharactersInRange:range withString:string];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *amountNumber = [numberFormatter numberFromString:textString];
if ([textString length] > 0) {
if (!amountNumber) {
return NO;
}
} else {
amountNumber = @0;
}
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *amountStringer = [numberFormatter stringFromNumber:amountNumber];
NSNumber *amountAgain = [numberFormatter numberFromString:amountStringer];
//
//make sure that the number obtained again is the same....prevents too many decimals....
if (![amountNumber isEqualToNumber:amountAgain]) {
return NO;
}
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
amountShow.text = amountStringer;
self.amount = [NSDecimalNumber decimalNumberWithDecimal:[amountAgain decimalValue]];
NSLog(@"decimal Amount is %@", self.amount);
return YES;
}
回答5:
For Swift 4 main answer of question is
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard textField == quantityTextField, let textFieldString = textField.text as NSString? else {
return true
}
let newString = textFieldString.replacingCharacters(in: range, with: string)
let expression = "^([0-9]+)?(\\.([0-9]{1,2})?)?$"
let regex = try? NSRegularExpression(pattern: expression, options: .caseInsensitive)
let numberOfMathces = regex?.numberOfMatches(in: newString, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, newString.characters.count))
if numberOfMathces == 0 {
return false
}
return true
}
回答6:
You can pass the string of uitexfield to below method, it will return yes/no..accordingly you can show error
- (BOOL) validatePhone: (NSString *) aMobile {
NSString *phoneRegex = @"^+(?:[0-9] ?){6,14}[0-9]$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
if (aMobile.length>0) {
NSString *mobileTextString = [[mobileText.text componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
NSString *firstNumber = [aMobile substringToIndex:1];
if (mobileTextString.length!=10){
return NO;
}
}
return [phoneTest evaluateWithObject:aMobile];
}