主要是对NSString的扩展验证相关
///////////////////////////// 正则表达式相关 ///////////////////////////////
#import "NSString+RegExp.h"
@implementationNSString(RegExp)
- (BOOL)isValidateWithRegex:(NSString*)regex
{
NSPredicate*pre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:self];
}
/** 字符串判空处理 */
-(BOOL)isNull;{
NSString*object = [selfcopy];
if([objectisEqual:@""]) {
return YES;
}
return NO;
}
/* 邮箱验证 MODIFIED BY HELENSONG */
- (BOOL)isValidEmail
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate*emailPred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return[emailPredevaluateWithObject:self];
}
/* 手机号码验证 MODIFIED BY HELENSONG */
- (BOOL)isValidPhoneNum
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";
NSPredicate*phonePred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return[phonePredevaluateWithObject:self];
}
/* 车牌号验证 MODIFIED BY HELENSONG */
- (BOOL)isValidCarNo
{
NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
NSPredicate*carPred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",carRegex];
return [carPred evaluateWithObject:self];
}
/** 网址验证 */
- (BOOL)isValidUrl
{
NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
return [self isValidateWithRegex:regex];
}
/** 邮政编码 */
- (BOOL)isValidPostalcode {
NSString *phoneRegex = @"^[0-8]\\d{5}(?!\\d)$";
NSPredicate*phonePred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return[phonePredevaluateWithObject:self];
}
/** 纯数字 */
- (BOOL)isValidNumber;{
NSString*numberRegex =@"[0-9]*";
NSPredicate*numberPred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",numberRegex];
return[numberPredevaluateWithObject:self];
}
/** 纯英文 */
- (BOOL)isValidEnglish;{
NSString*englishRegex =@"[a-zA-Z]*";
NSPredicate*englishPred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",englishRegex];
return[englishPredevaluateWithObject:self];
}
/** 仅输入字母或数字 */
- (BOOL)isValidEnglishOrNumber;{
NSString*engORnumRegex =@"[a-zA-Z0-9]*";
NSPredicate*engORnumPred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",engORnumRegex];
return[engORnumPredevaluateWithObject:self];
}
/** 纯汉字 */
- (BOOL)isValidChinese;
{
NSString *phoneRegex = @"^[\u4e00-\u9fa5]+$";
NSPredicate*phonePred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return[phonePredevaluateWithObject:self];
}
/**
@brief 是否符合IP格式,xxx.xxx.xxx.xxx
*/
- (BOOL)isValidIP;
{
NSString *regex = [NSString stringWithFormat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"];
NSPredicate*pre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
BOOL rc = [pre evaluateWithObject:self];
if(rc) {
NSArray *componds = [self componentsSeparatedByString:@","];
BOOLv =YES;
for(NSString*sincomponds) {
if(s.integerValue>255) {
v =NO;
break;
}
}
returnv;
}
return NO;
}
/** 身份证验证 refer to http://blog.csdn.net/afyzgh/article/details/16965107 */
- (BOOL)isValidIdCardNum
{
NSString*value = [selfcopy];
value = [valuestringByReplacingOccurrencesOfString:@"X" withString:@"x"];
value = [valuestringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
intlength =0;
if(!value) {
returnNO;
}else{
length = (int)value.length;
if(length !=15&& length !=18) {
returnNO;
}
}
// 省份代码
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 = [valuesubstringToIndex:2];
BOOLareaFlag =NO;
for(NSString*areaCodeinareasArray) {
if([areaCodeisEqualToString:valueStart2]) {
areaFlag =YES;
break;
}
}
if(!areaFlag) {
returnNO;
}
NSRegularExpression*regularExpression;
NSUIntegernumberofMatch;
intyear =0;
switch(length) {
case15:
year = [valuesubstringWithRange: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 = [regularExpressionnumberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
if(numberofMatch >0) {
returnYES;
}else{
returnNO;
}
case18:
year = [valuesubstringWithRange:NSMakeRange(6,4)].intValue;
if(year %4==0|| (year %100==0&& year %4==0)) {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[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|20[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:NSRegularExpressionCaseInsensitiveerror:nil];// 测试出生日期的合法性
}
numberofMatch = [regularExpressionnumberOfMatchesInString: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;
intY = S %11;
NSString*M =@"F";
NSString*JYM =@"10X98765432";
M = [JYMsubstringWithRange:NSMakeRange(Y,1)];// 判断校验位
if ([M isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]) {
returnYES;// 检测ID的校验位
}else{
returnNO;
}
}else{
returnNO;
}
default:
returnNO;
}
return NO;
}
/** 银行卡验证*/
- (BOOL)isBankCard;{
NSString*cardNumber = [selfcopy];
if(cardNumber.length==0){
returnNO;
}
NSString*digitsOnly =@"";
charc;
for(inti =0; i < cardNumber.length; i++){
c = [cardNumbercharacterAtIndex:i];
if(isdigit(c)){
digitsOnly =[digitsOnlystringByAppendingFormat:@"%c",c];
}
}
intsum =0;
intdigit =0;
intaddend =0;
BOOLtimesTwo =false;
for(NSIntegeri = digitsOnly.length-1; i >=0; i--){
digit = [digitsOnlycharacterAtIndex:i] -'0';
if(timesTwo)
{
addend = digit *2;
if(addend >9) {
addend -=9;
}
}else{
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}
intmodulus = sum %10;
returnmodulus ==0;
}
/**
@brief 是否符合最小长度、最长长度,是否包含中文,首字母是否可以为数字
@param minLenth 账号最小长度
@param maxLenth 账号最长长度
@param containChinese 是否包含中文
@param firstCannotBeDigtal 首字母不能为数字
@return 正则验证成功返回YES, 否则返回NO
*/
- (BOOL)isValidWithMinLenth:(NSInteger)minLenth
maxLenth:(NSInteger)maxLenth
containChinese:(BOOL)containChinese
firstCannotBeDigtal:(BOOL)firstCannotBeDigtal;
{
NSString*hanzi = containChinese ?@"\u4e00-\u9fa5":@"";
NSString*first = firstCannotBeDigtal ?@"^[a-zA-Z_]":@"";
NSString*regex = [NSStringstringWithFormat:@"%@[%@A-Za-z0-9_]{%d,%d}", first, hanzi, (int)(minLenth-1), (int)(maxLenth-1)];
NSPredicate*pre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject:self];
}
/**
@brief 是否符合最小长度、最长长度,是否包含中文,数字,字母,其他字符,首字母是否可以为数字
@param minLenth 账号最小长度
@param maxLenth 账号最长长度
@param containChinese 是否包含中文
@param containDigtal 包含数字
@param containLetter 包含字母
@param containOtherCharacter 其他字符
@param firstCannotBeDigtal 首字母不能为数字
@return 正则验证成功返回YES, 否则返回NO
*/
- (BOOL)isValidWithMinLenth:(NSInteger)minLenth
maxLenth:(NSInteger)maxLenth
containChinese:(BOOL)containChinese
containDigtal:(BOOL)containDigtal
containLetter:(BOOL)containLetter
containOtherCharacter:(NSString*)containOtherCharacter
firstCannotBeDigtal:(BOOL)firstCannotBeDigtal;
{
NSString*hanzi = containChinese ?@"\u4e00-\u9fa5":@"";
NSString*first = firstCannotBeDigtal ?@"^[a-zA-Z_]":@"";
NSString*lengthRegex = [NSStringstringWithFormat:@"(?=^.{%@,%@}$)",@(minLenth),@(maxLenth)];
NSString*digtalRegex = containDigtal ?@"(?=(.*\\d.*){1})":@"";
NSString*letterRegex = containLetter ?@"(?=(.*[a-zA-Z].*){1})":@"";
NSString*characterRegex = [NSStringstringWithFormat:@"(?:%@[%@A-Za-z0-9%@]+)", first, hanzi, containOtherCharacter ? containOtherCharacter :@""];
NSString*regex = [NSStringstringWithFormat:@"%@%@%@%@", lengthRegex, digtalRegex, letterRegex, characterRegex];
NSPredicate*pre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject:self];
}
/** 去掉两端空格和换行符 */
- (NSString*)stringByTrimmingBlank
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
/** 去掉html格式 */
- (NSString*)removeHtmlFormat;
{
NSString*str = [NSStringstringWithFormat:@"%@",self];
NSError*error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:&error];
if(!error) {
str = [regexstringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@"$2$1"];
}else{
NSLog(@"%@", error);
}
NSArray*html_code =@[
@"\"",@"'",@"&",@"<",@">",
@"",@"¡",@"¢",@"£",@"¤",
@"¥",@"¦",@"§",@"¨",@"©",
@"ª",@"«",@"¬",@"",@"®",
@"¯",@"°",@"±",@"²",@"³",
@"´",@"µ",@"¶",@"·",@"¸",
@"¹",@"º",@"»",@"¼",@"½",
@"¾",@"¿",@"×",@"÷",@"À",
@"Á",@"Â",@"Ã",@"Ä",@"Å",
@"Æ",@"Ç",@"È",@"É",@"Ê",
@"Ë",@"Ì",@"Í",@"Î",@"Ï",
@"Ð",@"Ñ",@"Ò",@"Ó",@"Ô",
@"Õ",@"Ö",@"Ø",@"Ù",@"Ú",
@"Û",@"Ü",@"Ý",@"Þ",@"ß",
@"à",@"á",@"â",@"ã",@"ä",
@"å",@"æ",@"ç",@"è",@"é",
@"ê",@"ë",@"ì",@"í",@"î",
@"ï",@"ð",@"ñ",@"ò",@"ó",
@"ô",@"õ",@"ö",@"ø",@"ù",
@"ú",@"û",@"ü",@"ý",@"þ",
@"ÿ",@"∀",@"∂",@"∃",@"∅",
@"∇",@"∈",@"∉",@"∋",@"∏",
@"∑",@"−",@"∗",@"√",@"∝",
@"∞",@"∠",@"∧",@"∨",@"∩",
@"∪",@"∫",@"∴",@"∼",@"≅",
@"≈",@"≠",@"≡",@"≤",@"≥",
@"⊂",@"⊃",@"⊄",@"⊆",@"⊇",
@"⊕",@"⊗",@"⊥",@"⋅",@"Α",
@"Β",@"Γ",@"Δ",@"Ε",@"Ζ",
@"Η",@"Θ",@"Ι",@"Κ",@"Λ",
@"Μ",@"Ν",@"Ξ",@"Ο",@"Π",
@"Ρ",@"Σ",@"Τ",@"Υ",@"Φ",
@"Χ",@"Ψ",@"Ω",@"α",@"β",
@"γ",@"δ",@"ε",@"ζ",@"η",
@"θ",@"ι",@"κ",@"λ",@"μ",
@"ν",@"ξ",@"ο",@"π",@"ρ",
@"ς",@"σ",@"τ",@"υ",@"φ",
@"χ",@"ψ",@"ω",@"ϑ",@"ϒ",
@"ϖ",@"Œ",@"œ",@"Š",@"š",
@"Ÿ",@"ƒ",@"ˆ",@"˜",@"",
@"",@"",@"",@"",@"",
@"",@"–",@"—",@"‘",@"’",
@"‚",@"“",@"”",@"„",@"†",
@"‡",@"•",@"…",@"‰",@"′",
@"″",@"‹",@"›",@"‾",@"€",
@"",@"←",@"↑",@"→",@"↓",
@"",@"↵",@"⌈",@"⌉",@"⌊",
@"⌋",@"",@"",@"",@"",
@"",
];
NSArray*code =@[
@""", @"'", @"&", @"<", @">",
@" ", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"", @"®",
@"¯", @"°", @"±", @"²", @"³",
@"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê",
@"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä",
@"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ",
@"ÿ", @"∀", @"∂", @"&exists;", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅",
@"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ",
@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ",
@"ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @" ",
@" ", @" ", @"", @"", @"",
@"", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€",
@"", @"←", @"↑", @"→", @"↓",
@"", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"", @"", @"", @"",
@"",
];
NSIntegeridx =0;
for(NSString*objincode) {
str = [strstringByReplacingOccurrencesOfString:(NSString *)obj withString:html_code[idx]];
idx++;
}
returnstr;
}
/** 工商税号 */
- (BOOL)isValidTaxNo
{
NSString *emailRegex = @"[0-9]\\d{13}([0-9]|X)$";
NSPredicate*emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return[emailTestevaluateWithObject:self];
}
/** 字符串是否含有emoji */
- (BOOL)isStringContrainsEmoji{
NSString*string = [selfcopy];
__blockBOOLreturnValue =NO;
[stringenumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
constunicharhs = [substringcharacterAtIndex:0];
if(0xd800<= hs && hs <=0xdbff) {
if(substring.length>1) {
constunicharls = [substringcharacterAtIndex:1];
constintuc = ((hs -0xd800) *0x400) + (ls -0xdc00) +0x10000;
if(0x1d000<= uc && uc <=0x1f77f) {
returnValue =YES;
}
}
}elseif(substring.length>1) {
constunicharls = [substringcharacterAtIndex:1];
if(ls ==0x20e3) {
returnValue =YES;
}
}else{
if(0x2100<= hs && hs <=0x27ff) {
returnValue =YES;
}elseif(0x2B05<= hs && hs <=0x2b07) {
returnValue =YES;
}elseif(0x2934<= hs && hs <=0x2935) {
returnValue =YES;
}elseif(0x3297<= hs && hs <=0x3299) {
returnValue =YES;
}elseif(hs ==0xa9|| hs ==0xae|| hs ==0x303d|| hs ==0x3030|| hs ==0x2b55|| hs ==0x2b1c|| hs ==0x2b1b|| hs ==0x2b50) {
returnValue =YES;
}
}
}];
returnreturnValue;
}
@end