//检查固定话
+ (BOOL)checkHomePhoneNumber:(NSString *) Number
{
// @"^[0]\\d{2}-\\d{8}|^[0]\\d{3}-\\d{7}|^[0]\\d{2}\\d{8}|^[0]\\d{3}\\d{7}"
// NSString *pattern = @"^[0]\\d{2}-\\d{8}|^[0]\\d{3}-\\d{7}|^[0]\\d{2}\\d{8}|^[0]\\d{3}\\d{7}|^[0]\\d{2}-\\d{9}|^[0]\\d{3}-\\d{8}|^[0]\\d{2}\\d{9}|^[0]\\d{3}\\d{8}";
NSString *pattern = @"^(0[0-9]{2,3})?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:Number];
return isMatch;
}
//6为数字密码
+ (BOOL)checkSixNumber:(NSString *) SixNumber
{
NSString *pattern = @"^[0-9]{6}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:SixNumber];
return isMatch;
}
//正则匹配手机号
+ (BOOL)checkTelNumber:(NSString *) telNumber
{
// NSString *pattern = @"^1+[3578]+\\d{9}";
// NSString *pattern = @"^1+[0123456789]+\\d{9}";
// NSString *pattern = @"^[1][0123456789][0-9]{9}$";
NSString *pattern = @"^1[3|4|5|7|8]+\\d{9}$";
// ^1[3|4|5|7|8]\d{9}$
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:telNumber];
return isMatch;
}
//正则匹配用户密码6-16位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
NSString *pattern = @"[a-zA-Z0-9~`!@#$%^&*()-_=+\{}|:;\"'<>,./]{6,16}";
// NSString *pattern = @"[a-zA-Z0-9]{6,16}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:password];
return isMatch;
}
//正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:userName];
return isMatch;
}
//正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
//[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx]
// ([0-9]{15}$)|([0-9]{17}([0-9]|X))|([0-9]{17}([0-9]|x))
NSString *pattern = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:idCard];
return isMatch;
}
#pragma 正则匹配用户身份证号15或18位
+ (BOOL)isCorrect:(NSString *)IDNumber
{
NSMutableArray *IDArray = [NSMutableArray array];
for (int i = 0; i < 18; i++) {
NSRange range = NSMakeRange(i, 1);
NSString *subString = [IDNumber substringWithRange:range];
[IDArray addObject:subString];
}
// 系数数组
NSArray *coefficientArray = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
// 余数数组
NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];
int sum = 0;
for (int i = 0; i < 17; i++) {
int coefficient = [coefficientArray[i] intValue];
int ID = [IDArray[i] intValue];
sum += coefficient * ID;
}
NSString *str = remainderArray[(sum % 11)];
NSString *string = [IDNumber substringFromIndex:17];
if ([str isEqualToString:string]) {
return YES;
} else {
return NO;
}
}
//正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
NSString *pattern = @"^[0-9]{12}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:number];
return isMatch;
}
//正则匹配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)checkFloat : (NSString *) floatStr
{
NSString *pattern = @"^[0-9]+([.]{0,1}[0-9]+){0,1}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:floatStr];
return isMatch;
}
//匹配正整数
+ (BOOL)checkunsignedInt: (NSString *) floatStr
{
NSString *pattern = @"^[0-9]{0,10}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:floatStr];
return isMatch;
}
// 匹配中文
+ (BOOL)checkChinese:(NSString *)chinese
{
NSString *pattern = @"^[\\u2E80-\\uFE4F]+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:chinese];
return isMatch;
}
//匹配金钱
+ (BOOL)checkMoney:(NSString *)money
{
NSString *pattern = @"^(?!0+(?:\\.0+)?$)(?:[1-9]\\d*|0)(?:\\.\\d{1,2})?$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:money];
return isMatch;
}
// 匹配邮箱
+ (BOOL)isValidateEmail:(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];
}