1.手机号11位
//中国移动
NSString * MOBILE = @"^1([3-9][0-9]|5[0-35-9]|8[025-9])\d{8}$";
//中国联通
NSString * CM = @"^1([3-8][0-8]|(3[5-9]|5[017-9]|8[278])\d)\d{7}$";
//中国电信
NSString * CU = @"^1([3-8][0-2]|5[256]|8[56])\d{8}$";
//大陆地区固话及小灵通
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\d{7}$";
2.固定电话
//验证输入的固话中不带 "-"符号
NSString * strNum = @"(0[0-9]{2,3})?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|((13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)";
//验证输入的固话中带 "-"符号
NSString * strNum = @"(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|((13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)";
3.社会信用代码
NSString *scN = @"^([0-9ABCDEFGHJKLMNPQRTUWXY]{2})([0-9]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-9Y])$";
4.匹配中文字符
NSString *string = @"[\u4e00-\u9fa5]";
5.匹配双字节字符(包括汉字)
NSString *string = @"[^\x00-\xff]";
6.匹配空白行
NSString string = @"\n\s\r";
7.匹配Email地址
NSString string = @"[\w!#$%&'+/=?^_{|}~-]+(?:\.[\w!#$%&'*+/=?^_
{|}~-]+)*@(?:\w?.)+\w?";
8.匹配网址URL
NSString string = @"[a-zA-z]+://[^\s]";
9.匹配腾讯QQ
NSString *string = @"[1-9][0-9]{4,}“;
10.匹配邮编
NSString *string = @"[1-9]\d{5}(?!\d)";
11.匹配18位身份证号
NSString *string = @"^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$";
12.匹配日期(年-月-日)格式
NSString *string = @"([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))";
13.匹配整数
NSString string = @"^[1-9]\d$”;
14.匹配负数
NSString string = @"^-[1-9]\d$";
15.除了特殊字符
NSString *str =@"^[A-Za-z0-9\u4e00-\u9fa5]+$";
16.长度大于6位后再接着判断是否同时包含数字和大小写字母
NSString * regex =@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
17.车型
NSString *regex2 = @"^[\u4E00-\u9FFF]+$";
18.车牌号
NSString *carRegex = @"^[\u4e00-\u9fff]{1}[a-zA-Z]{1}[-][a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fff]$";//其中\u4e00-\u9fa5表示unicode编码中汉字已编码部分,\u9fa5-\u9fff是保留部分,将来可能会添加
19.IP地址有效性
NSString *regex = [NSStringstringWithFormat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"];
20.Mac地址
NSString * macAddRegex = @"([A-Fa-f\\d]{2}:){5}[A-Fa-f\\d]{2}";
21.工商税号
NSString *taxNoRegex = @"[0-9]\\d{13}([0-9]|X)$";
举例用法:
+ (BOOL)isMobileNumber:(NSString *)telephoneNum{
//中国移动
NSString * MOBILE =@"^1([3-9][0-9]|5[0-35-9]|8[025-9])\\d{8}$";
//中国联通
NSString * CM = @"^1([3-8][0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
//中国电信
NSString * CU = @"^1([3-8][0-2]|5[256]|8[56])\\d{8}$";
//大陆地区固话及小灵通
// NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
NSPredicate *regextestmobile = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", CU];
if ((([regextestmobileevaluateWithObject:telephoneNum] == YES) || ([regextestcm evaluateWithObject:telephoneNum] ==YES) || ([regextestcu evaluateWithObject:telephoneNum] ==YES))&&telephoneNum.length ==11) {
return YES;
} else {
return NO;
}
}
网友评论