iOS中常用的正则验证
2017-03-25 本文已影响20人
yaoyao_IOS
@interface ZhengZeObject : NSObject
// 验证各大运营商电话号段
+ (BOOL)isMatchedByRegex:(NSString *)mobileNum;
// 判断输入的是否是手机号
+ (BOOL)isValidPhoneNumber:(NSString *)phoneNumber;
// 判断输入的邮箱格式是否正确
+ (BOOL)isValidEmail:(NSString *)email;
// 判断输入的身份证格式和合法性是否正确
+ (BOOL)isValidCardNumber:(NSString *)identityCard;
// 判断输入的车牌号格式是否正确
+ (BOOL)isValidCarID:(NSString *)carID;
// 判断输入6~16位数字/字母/下划线组成的密码格式是否正确
+ (BOOL)isValidPassword:(NSString *)password;
// 判断输入字符串是否全为[(数字)OR(数字|字母)OR(字母)OR(汉字)]
+ (BOOL)isValidAlltype:(NSString *)oneType;
// 判断输入字符串是否全为[(数字)OR(数字|字母)OR(字母)OR(汉字)]
+ (BOOL)isValidBankCard:(NSString *)bankCard;
// 正则去除网络标签
+ (NSString *)getInnerTextWithHTMLString:(NSString *)string;
// 验证各大运营商电话号段
+ (BOOL)isMatchedByRegex:(NSString *)mobileNum {
/**
* 是否是移动号
* 中国移动:China Mobile
* 134[0-8],13[5-9],147,15[0-2],15[7-9],170[3|5|6],178,18[2-4],18[7-8]
*/
NSString *CM = @"^1(34[0-8]|70[356]|(3[5-9]|4[7]|5[0-27-9]|7[8]|8[2-47-8])\\d)\\d{7}$";
/**
* 是否是联通号
* 中国联通:China Unicom
* 13[0-2],145,15[5-6],17[5-6],18[5-6],170[4|7|8|9],171
*/
NSString *CU = @"^1(70[07-9]|(3[0-2]|4[5]|5[5-6]|7[15-6]|8[5-6])\\d)\\d{7}$";
/**
* 是否是电信号
* 中国电信:China Telecom
* 133,1349,149,153,173,177,180,181,189,170[0-2]
*/
NSString *CT = @"^1(34[9]|70[0-2]|(3[3]|4[9]|5[3]|7[37]|8[019])\\d)\\d{7}$";
/**
* 大陆地区固话及小灵通
* 区号:010,020,021,022,023,024,025,027,028,029
* 号码:七位或八位
*/
NSString *MT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
NSPredicate *regextestmt = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MT];
if (([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestmt evaluateWithObject:mobileNum] == YES)) {
return YES;
}else{
return NO;
}
// 判断输入的是否是手机号
/**
手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9]
移动:134[0-8],13[5-9],147,15[0-2],15[7-9],178,18[2-4],18[7-8]
联通:13[0-2],145,15[5-6],17[5-6],18[5-6]
电信:133,1349,149,153,173,177,180,181,189
虚拟运营商: 170[0-2]电信 170[3|5|6]移动 170[4|7|8|9],171 联通
上网卡又称数据卡,14号段为上网卡专属号段,中国联通上网卡号段为145,中国移动上网卡号段为147,中国电信上网卡号段为149
*/
+ (BOOL)isValidPhoneNumber:(NSString *)mobileNum {
NSString * phoneRegex = @"^1(3[0-9]|4[579]|5[0-35-9]|7[01356]|8[0-9])\\d{8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
BOOL isMatch = [pred evaluateWithObject:mobileNum];
return isMatch;
}
+ (BOOL)isValidCardNumber:(NSString *)value {
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
long length = 0;
if (!value) {
return false;
} else {
length = value.length;
if (length != 15 && length != 18) {
return false;
}
}
// 省份代码
NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
NSString *valueStart2 = [value substringToIndex:2];
BOOL areaFlag = FALSE;
for (NSString *areaCode in areasArray) {
if ([areaCode isEqualToString:valueStart2]) {
areaFlag = YES;
break;
}
}
if (!areaFlag) {
return false;
}
NSRegularExpression *regularExpression;
NSUInteger numberofMatch;
int year = 0;
switch (length) {
case 15:
year = [value substringWithRange:NSMakeRange(6,2)].intValue + 1900;
if (year % 4 == 0 || (year % 100 == 0 && year % 4 == 0)) {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
options:NSRegularExpressionCaseInsensitive
error:nil];//测试出生日期的合法性
}else {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
options:NSRegularExpressionCaseInsensitive
error:nil];//测试出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value
options:NSMatchingReportProgress
range:NSMakeRange(0, value.length)];
if(numberofMatch >0) {
return YES;
}else {
return NO;
}
case 18:
year = [value substringWithRange:NSMakeRange(6,4)].intValue;
if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
options:NSRegularExpressionCaseInsensitive
error:nil];//测试出生日期的合法性
}else {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
options:NSRegularExpressionCaseInsensitive
error:nil];//测试出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value
options:NSMatchingReportProgress
range:NSMakeRange(0, value.length)];
if(numberofMatch > 0) {
int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
int Y = S %11;
NSString *M = @"F";
NSString *JYM = @"10X98765432";
M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
return YES;// 检测ID的校验位
}else {
return NO;
}
}else {
return NO;
}
default:
return false;
}
}
+ (BOOL)isValidEmail:(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)isValidCarID:(NSString *)carID {
if (carID.length!=7) {
return NO;
}
NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-hj-zA-HJ-Z]{1}[a-hj-zA-HJ-Z_0-9]{4}[a-hj-zA-HJ-Z_0-9_\u4e00-\u9fa5]$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
return [carTest evaluateWithObject:carID];
return YES;
}
+ (BOOL)isValidPassword:(NSString *)password {
NSString * pattern = @"^[A-Za-z0-9_]{6,16}$";
// NSString *condition =@"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,16}";
NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
return [pred evaluateWithObject:password];
}
+ (BOOL)isValidAlltype:(NSString *)oneType {
NSString *condition = @"^[0-9]*$"; //是否都是数字
// NSString *condition = @"^[A-Za-z]+$"; //是否都是字母
// NSString *condition = @"^[A-Za-z0-9]+$"; //是否都是字母和数字{6,16}
// NSString *condition = @"^[A-Za-z0-9]{6,16}$"; //是否都是字母和数字且长度在[6,16]
// NSString *condition = @"^[\u4e00-\u9fa5]{0,}$"; //只能输入汉字
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",condition];
return [predicate evaluateWithObject:oneType];
}
+ (BOOL)isValidBankCard:(NSString *)bankCard {
if (bankCard.length < 16) {
return NO;
}
NSInteger oddsum = 0; //奇数求和
NSInteger evensum = 0; //偶数求和
NSInteger allsum = 0;
NSInteger cardNoLength = (NSInteger)[bankCard length];
// 取了最后一位数
NSInteger lastNum = [[bankCard substringFromIndex:cardNoLength-1] intValue];
//测试的是除了最后一位数外的其他数字
bankCard = [bankCard substringToIndex:cardNoLength - 1];
for (NSInteger i = cardNoLength -1 ; i>=1;i--) {
NSString *tmpString = [bankCard substringWithRange:NSMakeRange(i-1, 1)];
NSInteger tmpVal = [tmpString integerValue];
if (cardNoLength % 2 ==1 ) {//卡号位数为奇数
if((i % 2) == 0){//偶数位置
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}else{//奇数位置
oddsum += tmpVal;
}
}else{
if((i % 2) == 1){
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}else{
oddsum += tmpVal;
}
}
}
allsum = oddsum + evensum;
allsum += lastNum;
if((allsum % 10) == 0)
return YES;
else
return NO;
}
// 正则去除网络标签
+ (NSString *)getInnerTextWithHTMLString:(NSString *)string{
NSRegularExpression * regularExpretion = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n| " options:0 error:nil];
string = [regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
return string;
}
附:
>1、校验密码强度
密码的强度必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
>2、校验中文
字符串只能是中文
^[\\u4e00-\\u9fa5]{0,}$
>3、由数字,26个英文字母或下划线组成的字符串
^\\w+$
>4、校验E-Mail 地址
[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?
>5、校验身份证号码
15位:
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$
18位:
^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$
>6、校验日期
“yyyy-mm-dd“ 格式的日期校验,已考虑平闰年
^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$
>7、校验金额
金额校验,精确到2位小数
^[0-9]+(.[0-9]{2})?$
>8、校验手机号
^1(3[0-9]|4[579]|5[0-35-9]|7[01356]|8[0-9])\\d{8}$
>9、判断IE的版本
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$
>10、校验IP-v4地址
\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b
>11、校验IP-v6地址
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
>12、检查URL的前缀
if (!s.match(/^[a-zA-Z]+:\\/\\//)){
s = 'http://' + s;}
>13、提取URL链接
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?
>14、文件路径及扩展名校验
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$
>15、提取Color Hex Codes
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
>16、提取网页图片
\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)
>17、提取页面超链接
(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>
>18、查找CSS属性
^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}
>19、抽取注释
<!--(.*?)-->
>20、匹配HTML标签
<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>
>21.校验基本日期格式
var reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/;