输入的Textfield.text应具有特定格式

时间:2017-04-26 05:09:59

标签: ios objective-c uitextfielddelegate

我有textfield,我希望以下列格式输入文字: aa #### 1234 我知道这应该在shouldChangeCharactersInRange的{​​{1}} 委托方法中完成。我很难理解每个字符的条件是如何实现的。请提出你的建议。提前谢谢!

3 个答案:

答案 0 :(得分:0)

在UITextField上创建类别,并根据您的要求添加此方法写入条件。以下是电子邮件ID的示例:

 - (BOOL)validateRegEx:(NSString*)regexString
 {
     NSError *error = NULL;
     NSRegularExpression *regex = [NSRegularExpression    regularExpressionWithPattern:regexString
                                                                      options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
     NSUInteger numberOfMatches = [regex numberOfMatchesInString:self.text
                                                    options:0
                                                      range:NSMakeRange(0, [self.text length])];

    return numberOfMatches > 0;
}

答案 1 :(得分:0)

试试这个

-(BOOL)checkString:(NSString *)str{
    NSString *regex1 = @"^[a-bA-B0-9]"; // chabge regex as per your needs
    NSPredicate *test1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
    return [test1 evaluateWithObject:str] ;
}

答案 2 :(得分:0)

有助于验证您的需求的表达式可能是:

^[a-z]{2}\s\d{4}\s\d{4}$

下面:

  
      
  • ^ - 显示字符串的开头
  •   
  • [a-z]{2} - 显示a-z恰好2次
  • 之间的任何字符   
  • \s - 一次显示空格字符。
  •   
  • \d{4} - 显示0-9之间的任何数字,恰好是4次。
  •   
  • $ - 显示字符串结尾
  •   

作为解决方法的一个例子可能是这个,这里棘手的部分是允许构造下的字符串:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSError *error = NULL;
    NSRegularExpression *regex = nil;

     NSMutableString *combinedText = [textField.text mutableCopy];
    [combinedText replaceCharactersInRange:range withString:string];

    switch (combinedText.length) {
        case 1:
        case 2:
            regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[a-z]{%ld}", (long)combinedText.length] options:NSRegularExpressionCaseInsensitive error:&error];
            break;
        case 3:
            regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-z]{2}\\s" options:NSRegularExpressionCaseInsensitive error:&error];
            break;
        case 4:
        case 5:
        case 6:
        case 7:
            regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[a-z]{2}\\s\\d{%ld}", (long)combinedText.length - 3] options:NSRegularExpressionCaseInsensitive error:&error];
            break;
        case 8:
            regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-z]{2}\\s\\d{4}\\s" options:NSRegularExpressionCaseInsensitive error:&error];
            break;
        case 9:
        case 10:
        case 11:
        case 12:
            regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[a-z]{2}\\s\\d{4}\\s\\d{%ld}$", (long)combinedText.length - 8] options:NSRegularExpressionCaseInsensitive error:&error];
            break;
        default:
            return false;
    }

    if(error) return false;        

    NSUInteger numberOfMatches = [regex numberOfMatchesInString:combinedText options:0 range:NSMakeRange(0, combinedText.length)];
    return numberOfMatches > 0 || string.length == 0;
}

希望有所帮助!