1. 判断全汉字
- (BOOL)inputShouldChinese:(NSString*)inputString {
if(inputString.length ==0) returnNO;
NSString*regex =@"[\u4e00-\u9fa5]+";
NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
2. 判断全数字
- (BOOL)inputShouldNumber:(NSString*)inputString {
if(inputString.length ==0) returnNO;
NSString*regex =@"[0-9]*";
NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
3. 判断全字母
- (BOOL)inputShouldLetter:(NSString*)inputString {
if(inputString.length ==0) return NO;
NSString*regex =@"[a-zA-Z]*";
NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
4. 判断仅输入字母或数字
- (BOOL)inputShouldLetterOrNum:(NSString*)inputString {
if(inputString.length ==0) returnNO;
NSString*regex =@"[a-zA-Z0-9]*";
NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
链接:https://www.jianshu.com/p/0180c4a765e5