- //邮箱
- + (BOOL) validateEmail:(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];
- }
- //手机号码验证
- + (BOOL) validateMobile:(NSString *)mobile
- {
- //手机号以13, 15,18开头,八个 \d 数字字符
- NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
- NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
- return [phoneTest evaluateWithObject:mobile];
- }
- //车牌号验证
- + (BOOL) validateCarNo:(NSString *)carNo
- {
- NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
- NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
- NSLog(@"carTest is %@",carTest);
- return [carTest evaluateWithObject:carNo];
- }
- //车型
- + (BOOL) validateCarType:(NSString *)CarType
- {
- NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
- NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
- return [carTest evaluateWithObject:CarType];
- }
- //用户名
- + (BOOL) validateUserName:(NSString *)name
- {
- NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
- NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
- BOOL B = [userNamePredicate evaluateWithObject:name];
- return B;
- }
- //密码
- + (BOOL) validatePassword:(NSString *)passWord
- {
- NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
- NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
- return [passWordPredicate evaluateWithObject:passWord];
- }
- //昵称
- + (BOOL) validateNickname:(NSString *)nickname
- {
- NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
- NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
- return [passWordPredicate evaluateWithObject:nickname];
- }
- //身份证号
- + (BOOL) validateIdentityCard: (NSString *)identityCard
- {
- BOOL flag;
- if (identityCard.length <= 0) {
- flag = NO;
- return flag;
- }
- NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
- NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
- return [identityCardPredicate evaluateWithObject:identityCard];
- }
#pragma 正则匹配手机号
+ (
BOOL
)checkTelNumber:(
NSString
*) telNumber
{
NSString
*pattern = @^1+[3578]+\d{9};
NSPredicate
*pred = [
NSPredicate
predicateWithFormat:
@SELF
MATCHES %@, pattern];
BOOL
isMatch = [pred evaluateWithObject:telNumber];
return
isMatch;
}
#pragma 正则匹配用户密码6-18位数字和字母组合
+ (
BOOL
)checkPassword:(
NSString
*) password
{
NSString
*pattern = @^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18};
NSPredicate
*pred = [
NSPredicate
predicateWithFormat:
@SELF
MATCHES %@, pattern];
BOOL
isMatch = [pred evaluateWithObject:password];
return
isMatch;
}
#pragma 正则匹配用户姓名,20位的中文或英文
+ (
BOOL
)checkUserName : (
NSString
*) userName
{
NSString
*pattern = @^[a-zA-Z一-龥]{1,20};
NSPredicate
*pred = [
NSPredicate
predicateWithFormat:
@SELF
MATCHES %@, pattern];
BOOL
isMatch = [pred evaluateWithObject:userName];
return
isMatch;
}
#pragma 正则匹配用户身份证号15或18位
+ (
BOOL
)checkUserIdCard: (
NSString
*) idCard
{
NSString
*pattern = @(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$);
NSPredicate
*pred = [
NSPredicate
predicateWithFormat:
@SELF
MATCHES %@, pattern];
BOOL
isMatch = [pred evaluateWithObject:idCard];
return
isMatch;
}
#pragma 正则匹员工号,12位的数字
+ (
BOOL
)checkEmployeeNumber : (
NSString
*) number
{
NSString
*pattern = @^[0-9]{12};
NSPredicate
*pred = [
NSPredicate
predicateWithFormat:
@SELF
MATCHES %@, pattern];
BOOL
isMatch = [pred evaluateWithObject:number];
return
isMatch;
}
#pragma 正则匹配URL
+ (
BOOL
)checkURL : (
NSString
*) url
{
NSString
*pattern = @^[0-9A-Za-z]{1,50};
NSPredicate
*pred = [
NSPredicate
predicateWithFormat:
@SELF
MATCHES %@, pattern];
BOOL
isMatch = [pred evaluateWithObject:url];
return
isMatch;
}
/**
* 功能:验证身份证是否合法
* 参数:输入的身份证号
*/
- (BOOL)isValidPersonID {
// 判断位数
if (self.length != 15 && self.length != 18) {
return NO;
}
NSString *carid = self;
long lSumQT = 0;
// 加权因子
int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 校验码
unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};
// 将15位身份证号转换成18位
NSMutableString *mString = [NSMutableString stringWithString:self];
if (self.length == 15) {
[mString insertString:@"19" atIndex:6];
long p = 0;
const char *pid = [mString UTF8String];
for (int i = 0; i<= 16; i++) {
p += (pid[i] - 48) * R[i];
}
int o = p % 11;
NSString *string_content = [NSString stringWithFormat:@"%c", sChecker[o]];
[mString insertString:string_content atIndex:[mString length]];
carid = mString;
}
// 判断地区码
NSString * sProvince = [carid substringToIndex:2];
if (![self areaCode:sProvince]) {
return NO;
}
// 判断年月日是否有效
// 年份
int strYear = [[self substringWithString:carid begin:6 end:4] intValue];
// 月份
int strMonth = [[self substringWithString:carid begin:10 end:2] intValue];
// 日
int strDay = [[self substringWithString:carid begin:12 end:2] intValue];
NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeZone:localZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",
strYear, strMonth, strDay]];
if (date == nil) {
return NO;
}
const char *PaperId = [carid UTF8String];
// 检验长度
if(18 != strlen(PaperId)) return NO;
// 校验数字
for (int i = 0; i < 18; i++) {
if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) ) {
return NO;
}
}
// 验证最末的校验码
for (int i=0; i<=16; i++) {
lSumQT += (PaperId[i]-48) * R[i];
}
if (sChecker[lSumQT%11] != PaperId[17] ) {
return NO;
}
return YES;
}