Textfield.text 输入的内容应具有特定格式
Textfield.text entered should have a specific format
我有一个 textfield
,我希望以以下格式在其中输入文本:
AA #### 1234
我知道这应该在 shouldChangeCharactersInRange
delegate 方法中完成 textfield
.nut 我无法理解如何在每个 [=18= 上实现条件] 提前给你suggestions.Thanks!
在 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;
}
试试这个
-(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] ;
}
有助于验证您的需求的表达式可能是:
^[a-z]{2}\s\d{4}\s\d{4}$
这里:
^
- Shows start of string
[a-z]{2}
- Shows any character between a-z exactly 2 times
\s
- Shows white space character one time.
\d{4}
- Shows any digit from 0-9, exactly 4 times.
$
- Shows end of string
一个解决方法的例子可能是这个,这里棘手的部分是允许正在构建的字符串:
- (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;
}
希望对您有所帮助!
我有一个 textfield
,我希望以以下格式在其中输入文本:
AA #### 1234
我知道这应该在 shouldChangeCharactersInRange
delegate 方法中完成 textfield
.nut 我无法理解如何在每个 [=18= 上实现条件] 提前给你suggestions.Thanks!
在 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;
}
试试这个
-(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] ;
}
有助于验证您的需求的表达式可能是:
^[a-z]{2}\s\d{4}\s\d{4}$
这里:
^
- Shows start of string[a-z]{2}
- Shows any character between a-z exactly 2 times\s
- Shows white space character one time.\d{4}
- Shows any digit from 0-9, exactly 4 times.$
- Shows end of string
一个解决方法的例子可能是这个,这里棘手的部分是允许正在构建的字符串:
- (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;
}
希望对您有所帮助!