UITextField 只接受一位小数点,小数点前最多 3 位,小数点后最多 2 位
UITextField accepting only one decimal point with max 3 digits before decimal point & max 2 digits after decimal point
我想限制UITextField
只接受一位小数。
小数点前最多允许 3 位数字,小数点后最多允许 2 位数字。
请注意,最小位数可以为 1,小数不能先输入。
如何实现?
您可以使用以下代码来实现相同的场景。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *expression = @"^([0-9]*)(\.([0-9]+)?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger noOfMatches = [regex numberOfMatchesInString:newStr options:0 range:NSMakeRange(0,[newStr length])];
if (noOfMatches==0)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(range.length + range.location > textField.text.length)
{
return NO;
}
if ([newStr containsString:@"."])
{
return newLength <= 6;
}
return newLength <= 3;
// return YES;
}
因为.
被认为是一个字符。所以总共是 6 个字符。您可以调整条件中的值。
这是使用 Swift 和 正则表达式:
实现的方法
设置文本字段的委托。这可以在代码或 IB 中完成。
要验证用户键入的文本,您可以将类似于以下内容的代码添加到您的委托中:
// Number of digits that are allowed before decimal point
let kMaximumIntegerDigits = 3
// Number of digits that are allowed after decimal point
let kMaximumFractionDigits = 2
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// The replacement string is empty when the user have removed some characters.
if string.isEmpty == true { return true }
// Get the current string the text field holds.
let currentText = textField.text ?? ""
// Check the limits.
if (string.characters.count + currentText.characters.count) > 6 { return false }
// Get the string we are expecting to obtain after the replacement.
var resultingText = currentText
resultingText.insertContentsOf(string.characters, at: currentText.startIndex.advancedBy(range.location))
// Check the final string with the help of the regular expression.
let regex = "^(?:(?:[0-9]{1,\(kMaximumIntegerDigits)}[.][0-9]{0,\(kMaximumFractionDigits)})|(?:[0-9]){1,\(kMaximumIntegerDigits)})$"
let regexRange = resultingText.rangeOfString(regex, options: .RegularExpressionSearch)
if regexRange == nil { return false }
return true
}
最后,当用户试图结束编辑会话时,您应该验证生成的文本。你可以这样做:
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
// Get the current input string.
guard let currentText = textField.text else { return false }
// Create the regular expression for the final check.
let regex = "^(?:(?:[0-9]{1,\(kMaximumIntegerDigits)}[.][0-9]{1,\(kMaximumFractionDigits)})|(?:[0-9]){1,\(kMaximumIntegerDigits)})$"
let regexRange = currentText.rangeOfString(regex, options: .RegularExpressionSearch)
if regexRange == nil {
// Show an alert to the user with the message that explains what the input is expected...
return false
}
// Make additional clean-up and finalize the editing session.
return true
}
非常感谢大家的帮助。通过参考这些答案,我将答案框在下面。
编辑
虽然文本字段具有例如462. & 用户触摸退格键结果为 462,理想情况下应为 46
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *strTextField = [textField.text stringByReplacingCharactersInRange:range withString:string];
// restrict textfield from entering ./0 at first place
if (strTextField.length > 0) {
NSString *theCharacterAtIndex0 = [NSString stringWithFormat:@"%c", [strTextField characterAtIndex:0]];
if ([theCharacterAtIndex0 isEqualToString:@"."] || [theCharacterAtIndex0 isEqualToString:@"0"]) {
return NO;
}
}
// NSLog(@"%@ , %@", textField.text, strTextField);
// automatically add decimal point after 3 digits entered
if (![textField.text containsString:@"."]) {
if (strTextField.length == MAX_LENGTH_BeforeDecimal && ![strTextField containsString:@"."]) {
strTextField = [strTextField stringByAppendingString:@"."];
textField.text = strTextField;
return NO;
}
}
// when decimal is deleted
if ([textField.text containsString:@"."]) {
if (![strTextField containsString:@"."]) {
int indeOfdecimal = (int)[textField.text rangeOfString:@"."].location;
NSString *strBeforeDecimal = [textField.text substringToIndex:indeOfdecimal];
textField.text = strBeforeDecimal;
}
}
NSArray *separator = [strTextField componentsSeparatedByString:@"."];
// to restrict textfield to single decimal
if([separator count] > 2 ) {
return NO;
}
if([separator count] >= 2) {
// restrict the max digits before & after decimal points
NSString *sepStr0 = [NSString stringWithFormat:@"%@",[separator objectAtIndex:0]];
NSString *sepStr1 = [NSString stringWithFormat:@"%@",[separator objectAtIndex:1]];
if ([sepStr0 length] > 3) {
return NO;
}
if ([sepStr1 length] > 2) {
return NO;
}
}
return YES;
}
使用这个正则表达式代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Only allow one decimal point and 2 digits after it
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^[0-9]{0,3}$*((\.|,)[0-9]{0,2})?$";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString options:0 range:NSMakeRange(0, [newString length])];
return numberOfMatches != 0;
return YES;
}
这里使用的正则表达式是"^[0-9]{0,3}$*((\.|,)[0-9]{0,2})?$".
这里数字3(第一个粗体)表示小数点前的字符,数字2(第二个粗体)表示小数点后的字符。
您可以使用这个正则表达式并根据您的要求创建规则。
我想限制UITextField
只接受一位小数。
小数点前最多允许 3 位数字,小数点后最多允许 2 位数字。
请注意,最小位数可以为 1,小数不能先输入。
如何实现?
您可以使用以下代码来实现相同的场景。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *expression = @"^([0-9]*)(\.([0-9]+)?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger noOfMatches = [regex numberOfMatchesInString:newStr options:0 range:NSMakeRange(0,[newStr length])];
if (noOfMatches==0)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if(range.length + range.location > textField.text.length)
{
return NO;
}
if ([newStr containsString:@"."])
{
return newLength <= 6;
}
return newLength <= 3;
// return YES;
}
因为.
被认为是一个字符。所以总共是 6 个字符。您可以调整条件中的值。
这是使用 Swift 和 正则表达式:
实现的方法设置文本字段的委托。这可以在代码或 IB 中完成。
要验证用户键入的文本,您可以将类似于以下内容的代码添加到您的委托中:
// Number of digits that are allowed before decimal point let kMaximumIntegerDigits = 3 // Number of digits that are allowed after decimal point let kMaximumFractionDigits = 2 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // The replacement string is empty when the user have removed some characters. if string.isEmpty == true { return true } // Get the current string the text field holds. let currentText = textField.text ?? "" // Check the limits. if (string.characters.count + currentText.characters.count) > 6 { return false } // Get the string we are expecting to obtain after the replacement. var resultingText = currentText resultingText.insertContentsOf(string.characters, at: currentText.startIndex.advancedBy(range.location)) // Check the final string with the help of the regular expression. let regex = "^(?:(?:[0-9]{1,\(kMaximumIntegerDigits)}[.][0-9]{0,\(kMaximumFractionDigits)})|(?:[0-9]){1,\(kMaximumIntegerDigits)})$" let regexRange = resultingText.rangeOfString(regex, options: .RegularExpressionSearch) if regexRange == nil { return false } return true }
最后,当用户试图结束编辑会话时,您应该验证生成的文本。你可以这样做:
func textFieldShouldEndEditing(textField: UITextField) -> Bool { // Get the current input string. guard let currentText = textField.text else { return false } // Create the regular expression for the final check. let regex = "^(?:(?:[0-9]{1,\(kMaximumIntegerDigits)}[.][0-9]{1,\(kMaximumFractionDigits)})|(?:[0-9]){1,\(kMaximumIntegerDigits)})$" let regexRange = currentText.rangeOfString(regex, options: .RegularExpressionSearch) if regexRange == nil { // Show an alert to the user with the message that explains what the input is expected... return false } // Make additional clean-up and finalize the editing session. return true }
非常感谢大家的帮助。通过参考这些答案,我将答案框在下面。
编辑
虽然文本字段具有例如462. & 用户触摸退格键结果为 462,理想情况下应为 46
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *strTextField = [textField.text stringByReplacingCharactersInRange:range withString:string];
// restrict textfield from entering ./0 at first place
if (strTextField.length > 0) {
NSString *theCharacterAtIndex0 = [NSString stringWithFormat:@"%c", [strTextField characterAtIndex:0]];
if ([theCharacterAtIndex0 isEqualToString:@"."] || [theCharacterAtIndex0 isEqualToString:@"0"]) {
return NO;
}
}
// NSLog(@"%@ , %@", textField.text, strTextField);
// automatically add decimal point after 3 digits entered
if (![textField.text containsString:@"."]) {
if (strTextField.length == MAX_LENGTH_BeforeDecimal && ![strTextField containsString:@"."]) {
strTextField = [strTextField stringByAppendingString:@"."];
textField.text = strTextField;
return NO;
}
}
// when decimal is deleted
if ([textField.text containsString:@"."]) {
if (![strTextField containsString:@"."]) {
int indeOfdecimal = (int)[textField.text rangeOfString:@"."].location;
NSString *strBeforeDecimal = [textField.text substringToIndex:indeOfdecimal];
textField.text = strBeforeDecimal;
}
}
NSArray *separator = [strTextField componentsSeparatedByString:@"."];
// to restrict textfield to single decimal
if([separator count] > 2 ) {
return NO;
}
if([separator count] >= 2) {
// restrict the max digits before & after decimal points
NSString *sepStr0 = [NSString stringWithFormat:@"%@",[separator objectAtIndex:0]];
NSString *sepStr1 = [NSString stringWithFormat:@"%@",[separator objectAtIndex:1]];
if ([sepStr0 length] > 3) {
return NO;
}
if ([sepStr1 length] > 2) {
return NO;
}
}
return YES;
}
使用这个正则表达式代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Only allow one decimal point and 2 digits after it
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^[0-9]{0,3}$*((\.|,)[0-9]{0,2})?$";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString options:0 range:NSMakeRange(0, [newString length])];
return numberOfMatches != 0;
return YES;
}
这里使用的正则表达式是"^[0-9]{0,3}$*((\.|,)[0-9]{0,2})?$".
这里数字3(第一个粗体)表示小数点前的字符,数字2(第二个粗体)表示小数点后的字符。
您可以使用这个正则表达式并根据您的要求创建规则。