一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等。
// 判断邮箱
isValid = [text isValidEmail];
// 判断手机号码
isValid = [text isValidPhoneNum];
// 判断ip
isValid = [text isValidIP];
// 判断身份证号
isValid = [text isValidIdCardNum];
// 判断账号
isValid = [text isValidWithMinLenth: maxLenth: containChinese:NO firstCannotBeDigtal:YES];
// 去掉html格式
text = [text removeHtmlFormat];
// 工商税号
isValid = [text isValidTaxNo];
封装文件:
.h
#import <Foundation/Foundation.h> @interface NSString (STRegex) ///////////////////////////// 正则表达式相关 /////////////////////////////// /** 邮箱验证 */
- (BOOL)isValidEmail; /** 手机号码验证 */
- (BOOL)isValidPhoneNum; /** 车牌号验证 */
- (BOOL)isValidCarNo; /** 网址验证 */
- (BOOL)isValidUrl; /** 邮政编码 */
- (BOOL)isValidPostalcode; /** 纯汉字 */
- (BOOL)isValidChinese; /**
@brief 是否符合IP格式,xxx.xxx.xxx.xxx
*/
- (BOOL)isValidIP; /** 身份证验证 */
- (BOOL)isValidIdCardNum; /**
@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; /**
@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 *)stringByTrimmingBlank; /** 去掉html格式 */
- (NSString *)removeHtmlFormat; /** 工商税号 */
- (BOOL)isValidTaxNo; @end
.m
#import "NSString+STRegex.h" @implementation NSString (STRegex) ///////////////////////////// 正则表达式相关 /////////////////////////////// - (BOOL)isValidateWithRegex:(NSString *)regex
{
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pre evaluateWithObject:self];
} /* 邮箱验证 MODIFIED BY HELENSONG */
- (BOOL)isValidEmail
{
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: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 *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:self];
} /* 车牌号验证 MODIFIED BY HELENSONG */
- (BOOL)isValidCarNo
{
NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
return [carTest evaluateWithObject:self];
} /** 网址验证 */
- (BOOL)isValidUrl
{
NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
return [self isValidateWithRegex:regex];
} /** 邮政编码 */
- (BOOL)isValidPostalcode {
NSString *phoneRegex = @"^[0-8]\\d{5}(?!\\d)$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:self];
} /** 纯汉字 */
- (BOOL)isValidChinese;
{
NSString *phoneRegex = @"^[\u4e00-\u9fa5]+$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:self];
} - (BOOL)isValidIP;
{
NSString *regex = [NSString stringWithFormat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL rc = [pre evaluateWithObject:self]; if (rc) {
NSArray *componds = [self componentsSeparatedByString:@","]; BOOL v = YES;
for (NSString *s in componds) {
if (s.integerValue > ) {
v = NO;
break;
}
} return v;
} return NO;
} /** 身份证验证*/
- (BOOL)isValidIdCardNum
{
NSString *value = [self copy];
value = [value stringByReplacingOccurrencesOfString:@"X" withString:@"x"];
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
int length = ;
if (!value) {
return NO;
}else {
length = (int)value.length;
if (length != && length !=) {
return NO;
}
}
// 省份代码
NSArray *areasArray =@[@"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @""];
NSString *valueStart2 = [value substringToIndex:];
BOOL areaFlag = NO;
for (NSString *areaCode in areasArray) {
if ([areaCode isEqualToString:valueStart2]) {
areaFlag = YES;
break;
}
}
if (!areaFlag) {
return NO;
}
NSRegularExpression *regularExpression;
NSUInteger numberofMatch;
int year = ;
switch (length) {
case :
year = [value substringWithRange:NSMakeRange(,)].intValue +;
if (year % == || (year % == && year % ==)) {
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(, value.length)];
if(numberofMatch > ) {
return YES;
}else {
return NO;
}
case :
year = [value substringWithRange:NSMakeRange(,)].intValue;
if (year % == || (year % == && year % ==)) {
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:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(, value.length)];
if(numberofMatch > ) {
int S = ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + ([value substringWithRange:NSMakeRange(,)].intValue + [value substringWithRange:NSMakeRange(,)].intValue) * + [value substringWithRange:NSMakeRange(,)].intValue * + [value substringWithRange:NSMakeRange(,)].intValue * + [value substringWithRange:NSMakeRange(,)].intValue *;
int Y = S % ;
NSString *M = @"F";
NSString *JYM = @"10X98765432";
M = [JYM substringWithRange:NSMakeRange(Y,)]; // 判断校验位
if ([M isEqualToString:[[value substringWithRange:NSMakeRange(,)] uppercaseString]]) {
return YES;// 检测ID的校验位
}else {
return NO;
}
}else {
return NO;
} default:
return NO;
}
return NO;
} - (BOOL)isValidWithMinLenth:(NSInteger)minLenth
maxLenth:(NSInteger)maxLenth
containChinese:(BOOL)containChinese
firstCannotBeDigtal:(BOOL)firstCannotBeDigtal;
{
// [\u4e00-\u9fa5A-Za-z0-9_]{4,20}
NSString *hanzi = containChinese ? @"\u4e00-\u9fa5" : @"";
NSString *first = firstCannotBeDigtal ? @"^[a-zA-Z_]" : @""; NSString *regex = [NSString stringWithFormat:@"%@[%@A-Za-z0-9_]{%d,%d}", first, hanzi, (int)(minLenth-), (int)(maxLenth-)];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject:self];
} - (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 = [NSString stringWithFormat:@"(?=^.{%@,%@}$)", @(minLenth), @(maxLenth)];
NSString *digtalRegex = containDigtal ? @"(?=(.*\\d.*){1})" : @"";
NSString *letterRegex = containLetter ? @"(?=(.*[a-zA-Z].*){1})" : @"";
NSString *characterRegex = [NSString stringWithFormat:@"(?:%@[%@A-Za-z0-9%@]+)", first, hanzi, containOtherCharacter ? containOtherCharacter : @""];
NSString *regex = [NSString stringWithFormat:@"%@%@%@%@", lengthRegex, digtalRegex, letterRegex, characterRegex];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject:self];
} /** 去掉两端空格和换行符 */
- (NSString *)stringByTrimmingBlank
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} /** 去掉html格式 */
- (NSString *)removeHtmlFormat;
{
NSString *str = [NSString stringWithFormat:@"%@", self]; NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:&error];
if (!error) {
str = [regex stringByReplacingMatchesInString:str options: range:NSMakeRange(, str.length) withTemplate:@"$2$1"];
} else {
NSLog(@"%@", error);
} NSArray *html_code = @[
@"\"", @"'", @"&", @"<", @">",
@"", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"", @"®",
@"¯", @"°", @"±", @"²", @"³", @"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê", @"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä", @"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ", @"ÿ", @"∀", @"∂", @"∃", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅", @"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ", @"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ", @"ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @"", @"", @"", @"", @"", @"",
@"", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€", @"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
];
NSArray *code = @[
@""", @"'", @"&", @"<", @">",
@" ", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"­", @"®",
@"¯", @"°", @"±", @"²", @"³", @"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê", @"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä", @"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ", @"ÿ", @"∀", @"∂", @"&exists;", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅", @"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ", @"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ", @"ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @" ", @" ", @" ", @"‌", @"‍", @"‎",
@"‏", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€", @"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
];
NSArray *code_hex = @[
@""", @"'", @"&", @"<", @">",
@" ", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"", @"®",
@"¯", @"°", @"±", @"²", @"³", @"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê", @"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä", @"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ", @"ÿ", @"∀", @"∂", @"∃", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅", @"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ", @"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"Λ", @"μ", @"Ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @" ", @" ", @" ", @"", @"", @"",
@"", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€", @"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
]; NSInteger idx = ;
for (NSString *obj in code) {
str = [str stringByReplacingOccurrencesOfString:(NSString *)obj withString:html_code[idx]];
idx++;
}
return str;
} /** 工商税号 */
- (BOOL)isValidTaxNo
{
NSString *emailRegex = @"[0-9]\\d{13}([0-9]|X)$";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
} @end