//校验密码
+(BOOL)checkPass:(NSString*)pass
{
if(pass==nil)
{
return NO;
}
NSString *regex = @"^[A-Za-z0-9]+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@ AND SELF.length > 5 AND SELF.length < 17", regex];
return [predicate evaluateWithObject:pass];
}
//校验电话号码
+(BOOL)checkIsPhone:(NSString*)str
{
NSString *regex = @"(13[0-9]|14[57]|15[0123456789]|18[023456789])\\d{8}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [predicate evaluateWithObject:str];
}
//校验是否是数字
+(BOOL)checkIsNumber:(NSString*)str
{
if(str==nil)
{
return NO;
}
NSString *regex = @"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";//@"^[0-9]+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@ ", regex];
return [predicate evaluateWithObject:str];
}
//身份证号
+ (BOOL)CheckIsIdentityCard: (NSString *)identityCard
{
//判断是否为空
if (identityCard==nil||identityCard.length <= 0) {
return NO;
}
//判断是否是18位,末尾是否是x
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
if(![identityCardPredicate evaluateWithObject:identityCard]){
return NO;
}
//判断生日是否合法
NSRange range = NSMakeRange(6,8);
NSString *datestr = [identityCard substringWithRange:range];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat : @"yyyyMMdd"];
if([formatter dateFromString:datestr]==nil){
return NO;
}
//判断校验位
if(identityCard.length==18)
{
NSArray *idCardWi= @[ @"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2" ]; //将前17位加权因子保存在数组里
NSArray * idCardY=@[ @"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2" ]; //这是除以11后,可能产生的11位余数、验证码,也保存成数组
int idCardWiSum=0; //用来保存前17位各自乖以加权因子后的总和
for(int i=0;i<17;i++){
idCardWiSum+=[[identityCard substringWithRange:NSMakeRange(i,1)] intValue]*[idCardWi[i] intValue];
}
int idCardMod=idCardWiSum%11;//计算出校验码所在数组的位置
NSString *idCardLast=[identityCard 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 intValue]==[idCardY[idCardMod] intValue]){
return YES;
}else{
return NO;
}
}
}
return NO;
}