1.驗證手機號
-(BOOL)valiMobile:(NSString *)mobile
{
mobile = [mobile stringByReplacingOccurrencesOfString:@"" withString:@""];
if (mobile.length != 11)
{
return NO;
}else{
/**
* 移動號段正則表達式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 聯(lián)通號段正則表達式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 電信號段正則表達式
*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
if (isMatch1 || isMatch2 || isMatch3) {
return YES;
}else{
return NO;
}
}
}
2.驗證郵箱
+ (BOOL)isAvailableEmail:(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];
}
3.驗證身份證18位
intcheckIDfromchar(char*ID );//判斷18身份證號
constintfactor[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };//加權因子
constintchecktable[] = { 1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2 };//校驗值對應表
intcheckIDfromchar(char*ID )
{
if(strlen(ID)!=18) {//驗證18位
return0;
}
intIDNumber[ 19 ];
for(inti = 0; i < 18; i ++ )//相當于類型轉換
IDNumber[ i ] = ID[ i ] - 48;
returncheckID( IDNumber, ID );
}
intcheckID(intIDNumber[],charID[] )
{
inti = 0;//i為計數
intchecksum = 0;
for( ; i < 17; i ++ )
checksum += IDNumber[ i ] *factor[ i ];
printf("aaa = %c, bbb = %d\n", ID[17],checktable[ checksum % 11 ]);
if( IDNumber[ 17 ] ==checktable[ checksum % 11 ] || ( ID[ 17 ] == 'x' &&checktable[ checksum % 11 ] == 'x' )||( ID[ 17 ] == 'X' &&checktable[ checksum % 11 ] == 'x' ))
return1;
else
return0;
}
4.驗證15位身份證
- (BOOL)shenfenzheng15{
if(self&& ![selfisEqualToString:@"0"]) {
NSString* id15 =@"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
return[selfisMatchedByRegex:id15];
}
returnNO;
}
5.驗證字符串全是數字
- (BOOL)isAllNumber
{
if(self&& ![selfisEqualToString:@""])
{
NSString*regex =@"^[0-9]+$";
return[selfisMatchedByRegex:regex];
}
returnNO;
}