.h文件
1 #import <Foundation/Foundation.h> 2 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文件
1 #import "Utils.h" 2 3 @implementation Utils 4 #pragma 正则匹配手机号 5 + (BOOL)checkTelNumber:(NSString *) telNumber 6 { 7 NSString *pattern = @"^1+[3578]+\\d{9}"; 8 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 9 BOOL isMatch = [pred evaluateWithObject:telNumber]; 10 return isMatch; 11 } 12 13 14 #pragma 正则匹配用户密码6-18位数字和字母组合 15 + (BOOL)checkPassword:(NSString *) password 16 { 17 NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"; 18 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 19 BOOL isMatch = [pred evaluateWithObject:password]; 20 return isMatch; 21 22 } 23 24 #pragma 正则匹配用户姓名,20位的中文或英文 25 + (BOOL)checkUserName : (NSString *) userName 26 { 27 NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}"; 28 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 29 BOOL isMatch = [pred evaluateWithObject:userName]; 30 return isMatch; 31 32 } 33 34 35 #pragma 正则匹配用户身份证号15或18位 36 + (BOOL)checkUserIdCard: (NSString *) idCard 37 { 38 NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)"; 39 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 40 BOOL isMatch = [pred evaluateWithObject:idCard]; 41 return isMatch; 42 } 43 44 #pragma 正则匹员工号,12位的数字 45 + (BOOL)checkEmployeeNumber : (NSString *) number 46 { 47 NSString *pattern = @"^[0-9]{12}"; 48 49 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 50 BOOL isMatch = [pred evaluateWithObject:number]; 51 return isMatch; 52 53 } 54 55 #pragma 正则匹配URL 56 + (BOOL)checkURL : (NSString *) url 57 { 58 NSString *pattern = @"^[0-9A-Za-z]{1,50}"; 59 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; 60 BOOL isMatch = [pred evaluateWithObject:url]; 61 return isMatch; 62 63 } 64 65 @end
时间: 2024-10-09 23:33:36