@interface NSString (Predicate)
//有效的电话号码
- (BOOL) isValidMobileNumber;
//有效的真实姓名
- (BOOL) isValidRealName;
//是否只有中文
- (BOOL) isOnlyChinese;
//有效的验证码(根据自家的验证码位数进行修改)
- (BOOL) isValidVerifyCode;
//有效的银行卡号
- (BOOL) isValidBankCardNumber;
//有效的邮箱
- (BOOL) isValidEmail;
//有效的字母数字密码
- (BOOL) isValidAlphaNumberPassword;
//检测有效身份证
//15位
- (BOOL) isValidIdentifyFifteen;
//18位
- (BOOL) isValidIdentifyEighteen;
//限制只能输入数字
- (BOOL) isOnlyNumber;
@end
@implementation NSString (Predicate)
- (BOOL) isValidMobileNumber {
NSString* const MOBILE = @"^1(3|4|5|7|8)\\d{9}$";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [predicate evaluateWithObject:self];
}
- (BOOL) isValidVerifyCode
{
NSString *pattern = @"^[0-9]{4}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
return [predicate evaluateWithObject:self];
}
- (BOOL) isValidRealName
{
NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{2,8}$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [predicate evaluateWithObject:self];
}
- (BOOL) isOnlyChinese
{
NSString * chineseTest=@"^[\u4e00-\u9fa5]{0,}$";
NSPredicate*chinesePredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",chineseTest];
return [chinesePredicate evaluateWithObject:self];
}
- (BOOL) isValidBankCardNumber {
NSString* const BANKCARD = @"^(\\d{16}|\\d{19})$";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", BANKCARD];
return [predicate evaluateWithObject:self];
}
- (BOOL) isValidEmail
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
- (BOOL) validateNickName
{
NSString *userNameRegex = @"^[A-Za-z0-9\u4e00-\u9fa5]{1,24}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
return [userNamePredicate evaluateWithObject:self];
}
- (BOOL) isValidAlphaNumberPassword
{
NSString *regex = @"^(?!\\d+$|[a-zA-Z]+$)\\w{6,12}$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [identityCardPredicate evaluateWithObject:self];
}
- (BOOL) isValidIdentifyFifteen
{
NSString * identifyTest=@"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
NSPredicate*identifyPredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",identifyTest];
return [identifyPredicate evaluateWithObject:self];
}
- (BOOL) isValidIdentifyEighteen
{
NSString * identifyTest=@"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
NSPredicate*identifyPredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",identifyTest];
return [identifyPredicate evaluateWithObject:self];
}
- (BOOL) isOnlyNumber
{
BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
int i = 0;
while (i < self.length) {
NSString * string = [self substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
res = NO;
break;
}
i++;
}
return res;
}
@end
网友评论