下面這個是判斷輸入是否爲6位數字和字母的組合
NSString * str = _tf.text;
NSError * error = NULL;
NSRegularExpression * exp = [NSRegularExpression regularExpressionWithPattern:@"[0-9a-zA-Z]{6,6}" options:NSRegularExpressionCaseInsensitive error:&error];
if (error) {
NSLog(@"%@",error);
}else {
NSTextCheckingResult * result = [exp firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (result) {
NSLog(@"%@",[str substringWithRange:result.range]);
}else {
NSLog(@"輸入失敗");
}
}
常見輸入判斷對應的正則表達式有: