需要对特殊的字符串进行一个判断,主要使用到Cocoa框架中的NSPredicate,用来进行模糊查询,有点类似SQL。
NSPredicate有多种使用方式
一.使用正则表达式,MATCHES进行匹配。
//1.判断是否是有效邮箱
+ (BOOL)isValidateEmail:(NSString *)email
{
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:email];
}
//2.判断是否有效电话号码
+ (BOOL)checkTel:(NSString *)str
{
if ([str length] != 11) {
return NO;
}
//修改电话号码限制规则
// NSString *regex = @"0{0,1}(13[0-9]|14[0-9]|15[0-9]|18[0-9])[0-9]{8}$";
NSString *regex = @"0{0,1}1[0-9]{10}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:str];
if (!isMatch) {
return NO;
}
return YES;
}
//3.判断是否有效身份证号(大家可以去搜下身份证的规则)
+(BOOL)isValidIDCardNum:(NSString *)cardNumStr
{
NSString *idNumRegex = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
NSPredicate *cardNumtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", idNumRegex];
if ([cardNumtest evaluateWithObject:cardNumStr]) {
//如果通过该验证,说明身份证格式正确,但准确性还需计算
if (cardNumStr.length == 18) {
//18位身份证号
NSArray *idCardWi = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil];//将前17位加权因子保存在数组里
NSArray *idCardY = [NSArray arrayWithObjects:@"1",@"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];//这是除以11后,可能产生的11位余数、验证码,也保存成数组
long sum = 0;
for (int i = 0; i< 17; i++) {
sum+=[[cardNumStr substringWithRange:NSMakeRange(i, 1)] intValue] * [idCardWi[i] intValue];
}
int idCardMod = sum % 11;//计算出校验码所在的位置
NSString *idCardLast = [cardNumStr substringWithRange:NSMakeRange(17, 1)];//得到最后一位身份证号码
//如果等于2,则说明校验码是10,身份证号码最后一位应该是X
if(idCardMod==2) {
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]) {
return YES;
}
else{
return NO;
}
}
else {
//用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
if([idCardLast isEqualToString:idCardY[idCardMod]]) {
return YES;
}
else {
return NO;
}
}
}
else {
//15位身份证号
return YES;
}
}
else {
return NO;
}
return YES;
}
//4.密码校验(6-16位字母+数字组合)
+ (BOOL)checkIsDigitalAndLetter:(NSString *)string
{
if (string.length < 6 || string.length > 16)
{
return NO;
}
NSString *regex = @"^[A-Za-z0-9]+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([predicate evaluateWithObject:string]) {
if ([self hasDigital:string] && [self hasLetter:string]) {
return YES;
}else{
return NO;
}
}else{
return NO;
}
}
+ (BOOL)hasLetter:(NSString *)string
{
for(int i = 0; i < string.length ; i++){
unichar a = [string characterAtIndex:i];
if ((a >= 'A' && a <= 'Z' ) || (a >= 'a' && a <= 'z')) {
return YES;
}
}
return NO;
}
+ (BOOL)hasDigital:(NSString *)string
{
for(int i = 0; i < string.length ; i++){
unichar a = [string characterAtIndex:i];
if ((a >= '0' && a <= '9' )) {
return YES;
}
}
return NO;
}
二.判断银行卡是否有效
+ (BOOL)checkCardNo:(NSString*) cardNo {
if ([CommonMethods isEmptyOrNull:cardNo]) {
return NO;
}
//去除空格
cardNo = [cardNo stringByReplacingOccurrencesOfString:@" "
withString:@""];
int oddsum = 0;//奇数求和
int evensum = 0;//偶数求和
int allsum = 0;
int cardNoLength = (int)[cardNo length];
int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];
cardNo = [cardNo substringToIndex:cardNoLength - 1];
for (int i = cardNoLength -1 ; i>=1;i--) {
NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];
int tmpVal = [tmpString intValue];
if (cardNoLength % 2 ==1 ) {
if((i % 2) == 0) {
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}
else {
oddsum += tmpVal;
}
}
else {
if((i % 2) == 1) {
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}
else {
oddsum += tmpVal;
}
}
}
allsum = oddsum + evensum;
allsum += lastNum;
return ((allsum % 10) == 0) ? YES : NO;
}
+(BOOL)isEmptyOrNull:(NSString *)str
{
if (!str || [str isKindOfClass:[NSNull class]])
{
// null object
return true;
} else {
NSString *trimedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimedString length] == 0) {
// empty string
return true;
}
else{
// is neither empty nor null
return false;
}
}
}