Email邮箱地址
if(StrIsEmpty(email)){
return NO;
}
NSString *emailRegex = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
return [pre evaluateWithObject:email];
身份证号码
if(!code || code.length != 18){
return NO;
}
// 编写正则表达式
NSString *regular = @"^\\d{6}(18|19|20)?\\d{2}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|[xX])$";
// 创建谓词对象并设定条件的表达式
NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
// 对字符串判断,该方法会返回BOOL值
if(![numberPre evaluateWithObject:code]){
//身份证号格式有误
return NO;
}
NSDictionary *cityDic = @{@"11":@"北京",
@"12":@"天津",
@"13":@"河北",
@"14":@"山西",
@"15":@"内蒙古",
@"21":@"辽宁",
@"22":@"吉林",
@"23":@"黑龙江",
@"31":@"上海",
@"32":@"江苏",
@"33":@"浙江",
@"34":@"安徽",
@"35":@"福建",
@"36":@"江西",
@"37":@"山东",
@"41":@"河南",
@"42":@"湖北",
@"43":@"湖南",
@"44":@"广东",
@"45":@"广西",
@"46":@"海南",
@"50":@"重庆",
@"51":@"四川",
@"52":@"贵州",
@"53":@"云南",
@"54":@"西藏 ",
@"61":@"陕西",
@"62":@"甘肃",
@"63":@"青海",
@"64":@"宁夏",
@"65":@"新疆",
@"71":@"台湾",
@"81":@"香港",
@"82":@"澳门",
@"91":@"国外"};
NSString *codeCity = [cityDic objectForKey:[code substringToIndex:2]];
if(!codeCity){
//身份证号地址编码有误//
return NO;
}
//加权因子
NSArray *factor = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
//校验位
NSArray *parity = @[@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
int sum = 0;
int ai = 0;
int wi = 0;
for(int i = 0;i < 17;i ++){
unichar c = [code characterAtIndex:i];
NSString *theString1 = [NSString stringWithFormat:@"%c", c];
ai = [theString1 intValue];
wi = [[factor objectAtIndex:i] intValue];
sum += ai * wi;
}
NSString *lastCode = [NSString stringWithFormat:@"%c", [code characterAtIndex:17]];
if(![[parity objectAtIndex:sum%11] isEqualToString:[lastCode uppercaseString]]){
//身份证号校验位错误
return NO;
}
return YES;
手机号码(网上有很多区分移动、联通和电信的判断方法,但是在实际项目中并不能满足日益增多的号码格式,直接判断是否为13,18等开头会导致用户无法注册/登录,因此我这里根本未使用正则,只要满足1开头+11位即可,不过这也是建立在产品只针对国内用户而言,需要国际化切忌使用该方法)
if(StrIsEmpty(mobileNumber)){
return NO;
}
NSString *firstChar = [mobileNumber substringToIndex:1];
if([firstChar isEqualToString:@"1"] && mobileNumber.length == 11){
return YES;
}
return NO;