iOS常用的方法

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}$";

/**

* 联通号段正则表达式

*/

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;

}

你可能感兴趣的:(iOS常用的方法)