.h文件
1 #import2 3 @interface Utils : NSObject 4 #pragma 正则匹配手机号 5 + (BOOL)checkTelNumber:(NSString *) telNumber; 6 #pragma 正则匹配用户密码6-18位数字和字母组合 7 + (BOOL)checkPassword:(NSString *) password; 8 #pragma 正则匹配用户姓名,20位的中文或英文 9 + (BOOL)checkUserName : (NSString *) userName; 10 #pragma 正则匹配用户身份证号 11 + (BOOL)checkUserIdCard: (NSString *) idCard; 12 #pragma 正则匹员工号,12位的数字 13 + (BOOL)checkEmployeeNumber : (NSString *) number; 14 #pragma 正则匹配URL 15 + (BOOL)checkURL : (NSString *) url; 16 @end
.m文件
#import "Utils.h" @implementation Utils #pragma 正则匹配手机号 + (BOOL)checkTelNumber:(NSString *) telNumber { 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; } } } #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\u4E00-\u9FA5]{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; } @end
判断用户密码是否规范
#pragma 正则匹配密码 +(BOOL)judgePassWordLegal:(NSString *)pass;/*** 判断用户输入的密码是否符合规范,符合规范的密码要求:1. 长度6 ~ 102. 密码中必须同时包含数字和字母 ^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$ 分开来注释一下: ^ 匹配一行的开头位置 (?![0-9]+$) 预测该位置后面不全是数字 (?![a-zA-Z]+$) 预测该位置后面不全是字母 [0-9A-Za-z] {6,10} 由6-10位数字或这字母组成 $ 匹配行结尾位置 注:(?!xxxx) 是正则表达式的负向零宽断言一种形式,标识预该位置后不是xxxx字符。 */+(BOOL)judgePassWordLegal:(NSString *)pass { BOOL result = false; if ([pass length] >= 6 && [pass length] <= 10){ // 判断长度大于8位后再接着判断是否同时包含数字和字符 NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; result = [pred evaluateWithObject:pass]; } return result;}