直接上代碼 這里面包含很多的正則匹配
.h
/**
* 驗證字符串是否為空值
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsEmpty:(NSString *)targetString;
/**
* 驗證字符串是否為非負(fù)整數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNonnegativeInteger:(NSString *)targetString;
/**
* 驗證字符串是否為正整數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsPositiveInteger:(NSString *)targetString;
/**
* 驗證字符串是否為非正整數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNonPositiveInteger:(NSString *)targetString;
/**
* 驗證字符串是否為負(fù)整數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNegativeInteger:(NSString *)targetString;
/**
* 驗證字符串是否為整數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsInteger:(NSString *)targetString;
/**
* 驗證字符串是否為非負(fù)浮點數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNonNegativeFloatingPoint:(NSString *)targetString;
/**
* 驗證字符串是否為正浮點數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsPositiveFloatingPoint:(NSString *)targetString;
/**
* 驗證字符串是否為非正浮點數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNonPositiveFloatingPoint:(NSString *)targetString;
/**
* 驗證字符串是否為負(fù)浮點數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNegativeFloatingPoint:(NSString *)targetString;
/**
* 驗證字符串是否為浮點數(shù)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsFloatingPoint:(NSString *)targetString;
/**
* 驗證字符串是否為由26個英文字母組成的字符串
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIs26EnglishLetter:(NSString *)targetString;
/**
* 驗證字符串是否為由26個英文字母的大寫組成的字符串
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIs26CapitalEnglishLetter:(NSString *)targetString;
/**
* 驗證字符串是否為由26個英文字母的小寫組成的字符串
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIs26LowercaseEnglishLetter:(NSString *)targetString;
/**
* 驗證字符串是否為由數(shù)字和26個英文字母組成的字符串
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNumbersAnd26EnglishLetter:(NSString *)targetString;
/**
* 驗證字符串是否為由數(shù)字、26個英文字母或者下劃線組成的字符串
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsNumbersAnd26EnglishLetterAndBottomLine:(NSString *)targetString;
/**
* 驗證字符串是否為合法的郵箱email
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidMailbox:(NSString *)targetString;
/**
* 驗證字符串是否為合法的電話號碼
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidPhoneNumber:(NSString *)targetString;
/**
* 驗證字符串是否為合法的IP地址
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidIPAdress:(NSString *)targetString;
/**
* 驗證字符串是否為中文
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsChineseText:(NSString *)targetString;
/**
* 驗證字符串是否為雙字節(jié)字符(包括漢字在內(nèi))
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsDoubleByteCharacter:(NSString *)targetString;
/**
* 驗證字符串是否為空行
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsBlankLine:(NSString *)targetString;
/**
* 驗證字符串是合法的url
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidURL:(NSString *)targetString;
/**
* 驗證字符串是符合時間格式:年-月-日
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsDate_YearMonthDay:(NSString *)targetString;
/**
* 驗證字符串是符合時間格式:月/日/年
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsDate_MonthDayYear:(NSString *)targetString;
/**
* 驗證字符串是否為HTML標(biāo)記
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsHTML:(NSString *)targetString;
/**
* 驗證帳號是否合法(字母開頭戚宦,允許5-16字節(jié)杀狡,允許字母數(shù)字下劃線)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsLegalUserName:(NSString *)targetString;
/**
* 驗證用戶密碼(正確格式為: 以字母開頭,長度在5~17 之間胁附,只能包含字符、數(shù)字和下劃線)
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsLegalPassword:(NSString *)targetString;
/**
* 驗證是否為國內(nèi)電話號碼
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsDomesticTelephoneNumber:(NSString *)targetString;
/**
* 驗證QQ號
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsValidQQ:(NSString *)targetString;
/**
* 驗證是否只是數(shù)字
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsOnlyNumber:(NSString *)targetString;
/**
* 驗證只能輸入n位的數(shù)字
*
* @param targetString 目標(biāo)字符串
* @param figure 位數(shù)
* @return YES/NO
*/
-(BOOL)validateIsOnlyManyNumber:(NSString *)targetString withFigure:(NSInteger)figure;
/**
* 驗證只能輸入至少n位的數(shù)字
*
* @param targetString 目標(biāo)字符串
* @param figure 位數(shù)
* @return YES/NO
*/
-(BOOL)validateIsOnlyLeastManyNumber:(NSString *)targetString withFigure:(NSInteger)figure;
/**
* 驗證只能輸入m~n位的數(shù)字
*
* @param targetString 目標(biāo)字符串
* @param fromFigure m位
* @param toFigure n位
* @return YES/NO
*/
-(BOOL)validateIsOnlyNumber:(NSString *)targetString fromFigure:(NSInteger)fromFigure toFigure:(NSInteger)toFigure;
/**
* 驗證是否包含有 ^%&',;=?$/"等字符
*
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
-(BOOL)validateIsSpecialCharacters:(NSString *)targetString;
/**
* 實例方法自定義正則驗證
*
* @param customRegex 自定義的正則表達(dá)式
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
- (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString;
//====================================類方法========================================//
/**
* 驗證郵箱
*
* @param email 郵箱
*
* @return YES/NO
*/
+ (BOOL)validateEmail:(NSString *)email;
/**
* 驗證手機號
*
* @param mobile 手機號
*
* @return YES/NO
*/
+ (BOOL)validateMobile:(NSString *)mobile;
/**
* 驗證車牌號
*
* @param carNo 車牌號
*
* @return YES/NO
*/
+ (BOOL)validateCarNo:(NSString *)carNo;
/**
* 驗證車型
*
* @param CarType 車型
*
* @return YES/NO
*/
+ (BOOL)validateCarType:(NSString *)CarType;
/**
* 驗證用戶名
*
* @param name 用戶名
*
* @return YES/NO
*/
+ (BOOL)validateUserName:(NSString *)name;
/**
* 驗證密碼
*
* @param passWord 密碼
*
* @return YES/NO
*/
+ (BOOL)validatePassword:(NSString *)passWord;
/**
* 驗證昵稱
*
* @param nickname 昵稱
*
* @return YES/NO
*/
+ (BOOL)validateNickname:(NSString *)nickname;
/**
* 驗證身份證
*
* @param identityCard 身份證
*
* @return YES/NO
*/
+ (BOOL)validateIdentityCard:(NSString *)identityCard;
/**
* 驗證銀行卡號
*
* @param bankCardNumber 銀行卡號
*
* @return YES/NO
*/
+ (BOOL)validateBankCardNumber:(NSString *)bankCardNumber;
/**
* 驗證銀行卡后四位
*
* @param bankCardNumber 銀行卡后四位
*
* @return YES/NO
*/
+ (BOOL)validateBankCardLastNumber:(NSString *)bankCardNumber;
/**
* 驗證CVN
*
* @param cvnCode CVN
*
* @return YES/NO
*/
+ (BOOL)validateCVNCode:(NSString *)cvnCode;
/**
* 驗證月份
*
* @param month 月份
*
* @return YES/NO
*/
+ (BOOL)validateMonth:(NSString *)month;
/**
* 驗證年
*
* @param year 年
*
* @return YES/NO
*/
+ (BOOL)validateYear:(NSString *)year;
/**
* 驗證驗證碼
*
* @param verifyCode 驗證碼
*
* @return YES/NO
*/
+ (BOOL)validateVerifyCode:(NSString *)verifyCode;
/**
* 類方法自定義正則驗證
*
* @param customRegex 自定義的正則表達(dá)式
* @param targetString 目標(biāo)字符串
*
* @return YES/NO
*/
+ (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString;
@end
.m
//最終正則匹配結(jié)果
-(BOOL)regexPatternResultWithRegex:(NSString *)regex TargetString:(NSString *)targetString{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [predicate evaluateWithObject:targetString];
}
//驗證字符串是否為空值
-(BOOL)validateIsEmpty:(NSString *)targetString{
return targetString.length==0?YES:NO;
}
//驗證字符串是否為非負(fù)整數(shù)
-(BOOL)validateIsNonnegativeInteger:(NSString *)targetString{
NSString *regex = @"^\\d+$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
// 驗證字符串是否為正整數(shù)
-(BOOL)validateIsPositiveInteger:(NSString *)targetString{
NSString *regex = @"^[0-9]*[1-9][0-9]*$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為非正整數(shù)
-(BOOL)validateIsNonPositiveInteger:(NSString *)targetString{
NSString *regex = @"^((-\\d+)|(0+))$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為負(fù)整數(shù)
-(BOOL)validateIsNegativeInteger:(NSString *)targetString{
NSString *regex = @"^-[0-9]*[1-9][0-9]*$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為整數(shù)
-(BOOL)validateIsInteger:(NSString *)targetString{
NSString *regex = @"^-?\\d+$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為非負(fù)浮點數(shù)
-(BOOL)validateIsNonNegativeFloatingPoint:(NSString *)targetString{
NSString *regex = @"^\\d+(\\.\\d+)?$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為正浮點數(shù)
-(BOOL)validateIsPositiveFloatingPoint:(NSString *)targetString{
NSString *regex = @"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為非正浮點數(shù)
-(BOOL)validateIsNonPositiveFloatingPoint:(NSString *)targetString{
NSString *regex = @"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為負(fù)浮點數(shù)
-(BOOL)validateIsNegativeFloatingPoint:(NSString *)targetString;{
NSString *regex = @"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為浮點數(shù)
-(BOOL)validateIsFloatingPoint:(NSString *)targetString{
NSString *regex = @"^(-?\\d+)(\\.\\d+)?$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為由26個英文字母組成的字符串
-(BOOL)validateIs26EnglishLetter:(NSString *)targetString{
NSString *regex = @"^[A-Za-z]+$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為由26個英文字母的大寫組成的字符串
-(BOOL)validateIs26CapitalEnglishLetter:(NSString *)targetString{
NSString *regex = @"^[A-Z]+$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為由26個英文字母的小寫組成的字符串
-(BOOL)validateIs26LowercaseEnglishLetter:(NSString *)targetString{
NSString *regex = @"^[a-z]+$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為由數(shù)字和26個英文字母組成的字符串
-(BOOL)validateIsNumbersAnd26EnglishLetter:(NSString *)targetString{
NSString *regex = @"^[A-Za-z0-9]+$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為由數(shù)字滓彰、26個英文字母或者下劃線組成的字符串
-(BOOL)validateIsNumbersAnd26EnglishLetterAndBottomLine:(NSString *)targetString{
NSString *regex = @"^[0-9a-zA-Z_]{1,}$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為有效的郵箱email
-(BOOL)validateIsValidMailbox:(NSString *)targetString{
//NSString *regex = @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為有效的電話號碼
-(BOOL)validateIsValidPhoneNumber:(NSString *)targetString{
NSString *regex = @"^(\\d+-)?(\\d{4}-?\\d{7}|\\d{3}-?\\d{8}|^\\d{7,8})(-\\d+)?$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為有效的ip地址
-(BOOL)validateIsValidIPAdress:(NSString *)targetString{
NSString *regex = @"^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為中文
-(BOOL)validateIsChineseText:(NSString *)targetString{
NSString *regex = @"^[\u4E00-\u9FA5]*$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為雙字節(jié)字符(包括漢字在內(nèi))
-(BOOL)validateIsDoubleByteCharacter:(NSString *)targetString{
NSString *regex = @"^[\\x00-\\xff]$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為空行
-(BOOL)validateIsBlankLine:(NSString *)targetString{
NSString *regex = @"^(\\n[\\s| ]*\\r)$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是合法的url
-(BOOL)validateIsValidURL:(NSString *)targetString{
NSString *regex = @"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是符合時間格式:年-月-日
-(BOOL)validateIsDate_YearMonthDay:(NSString *)targetString{
NSString *regex = @"^(\\d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是符合時間格式:月/日/年
-(BOOL)validateIsDate_MonthDayYear:(NSString *)targetString{
NSString *regex = @"^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))\\(\\d{2}|\\d{4})$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證字符串是否為HTML標(biāo)記
-(BOOL)validateIsHTML:(NSString *)targetString{
NSString *regex = @"^(\\<(.*)>.*<\\\1>|<(.*) \\>\\)$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證帳號是否合法(字母開頭控妻,允許5-16字節(jié),允許字母數(shù)字下劃線)
-(BOOL)validateIsLegalUserName:(NSString *)targetString{
NSString *regex = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證用戶密碼(正確格式為: 以字母開頭揭绑,長度在5~17 之間弓候,只能包含字符、數(shù)字和下劃線)
-(BOOL)validateIsLegalPassword:(NSString *)targetString{
NSString *regex = @"^[a-zA-Z]\\w{5,17}$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證是否為國內(nèi)電話號碼
-(BOOL)validateIsDomesticTelephoneNumber:(NSString *)targetString{
NSString *regex = @"^((\\d{3}-|\\d{4}-)?(\\d{8}|\\d{7})?)$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證QQ號
-(BOOL)validateIsValidQQ:(NSString *)targetString{
NSString *regex = @"^[1-9]*[1-9][0-9]*$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證是否只是數(shù)字
-(BOOL)validateIsOnlyNumber:(NSString *)targetString{
NSString *regex = @"^[0-9]*$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證只能輸入n位的數(shù)字
-(BOOL)validateIsOnlyManyNumber:(NSString *)targetString withFigure:(NSInteger)figure{
NSString *regex = [NSString stringWithFormat:@"^\\d{%ld}$",figure];
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證只能輸入至少n位的數(shù)字
-(BOOL)validateIsOnlyLeastManyNumber:(NSString *)targetString withFigure:(NSInteger)figure{
NSString *regex = [NSString stringWithFormat:@"^\\d{%ld,}$",figure];
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證只能輸入m~n位的數(shù)字
-(BOOL)validateIsOnlyNumber:(NSString *)targetString fromFigure:(NSInteger)fromFigure toFigure:(NSInteger)toFigure{
NSString *regex = [NSString stringWithFormat:@"^\\d{%ld,%ld}$",fromFigure,toFigure];
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//驗證是否包含有 ^%&',;=?$/"等字符
-(BOOL)validateIsSpecialCharacters:(NSString *)targetString{
NSString *regex = @"^([~!/@#$%^&*()-_=+\\|[{}];:\'\",<.>/?]+)$";
return [self regexPatternResultWithRegex:regex TargetString:targetString];
}
//實例方法自定義正則驗證
- (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString{
return [self regexPatternResultWithRegex:customRegex TargetString:targetString];
}
//====================================類方法========================================//
//郵箱
+ (BOOL)validateEmail:(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)validateMobile:(NSString *)mobile
{
//手機號以13他匪, 15菇存,18開頭,八個 \d 數(shù)字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//車牌號驗證
+ (BOOL)validateCarNo:(NSString *)carNo
{
NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
NSLog(@"carTest is %@",carTest);
return [carTest evaluateWithObject:carNo];
}
//車型
+ (BOOL)validateCarType:(NSString *)CarType
{
NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
return [carTest evaluateWithObject:CarType];
}
//用戶名
+ (BOOL)validateUserName:(NSString *)name
{
NSString *userNameRegex = @"^[A-Za-z0-9]{4,20}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
BOOL B = [userNamePredicate evaluateWithObject:name];
return B;
}
//密碼
+ (BOOL)validatePassword:(NSString *)passWord
{
NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicate evaluateWithObject:passWord];
}
//昵稱
+ (BOOL)validateNickname:(NSString *)nickname
{
NSString *nicknameRegex = @"([\u4e00-\u9fa5]{2,5})(·[\u4e00-\u9fa5]{2,5})*";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [passWordPredicate evaluateWithObject:nickname];
}
//身份證號
+ (BOOL)validateIdentityCard: (NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
}
//銀行卡
+ (BOOL)validateBankCardNumber: (NSString *)bankCardNumber
{
BOOL flag;
if (bankCardNumber.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{15,30})";
NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [bankCardPredicate evaluateWithObject:bankCardNumber];
}
//銀行卡后四位
+ (BOOL)validateBankCardLastNumber: (NSString *)bankCardNumber
{
BOOL flag;
if (bankCardNumber.length != 4) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{4})";
NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [bankCardPredicate evaluateWithObject:bankCardNumber];
}
//CVN
+ (BOOL)validateCVNCode: (NSString *)cvnCode
{
BOOL flag;
if (cvnCode.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{3})";
NSPredicate *cvnCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [cvnCodePredicate evaluateWithObject:cvnCode];
}
//month
+ (BOOL)validateMonth: (NSString *)month
{
BOOL flag;
if (!(month.length == 2)) {
flag = NO;
return flag;
}
NSString *regex2 = @"(^(0)([0-9])$)|(^(1)([0-2])$)";
NSPredicate *monthPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [monthPredicate evaluateWithObject:month];
}
//year
+ (BOOL) validateYear: (NSString *)year
{
BOOL flag;
if (!(year.length == 2)) {
flag = NO;
return flag;
}
NSString *regex2 = @"^([1-3])([0-9])$";
NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [yearPredicate evaluateWithObject:year];
}
//verifyCode
+ (BOOL) validateVerifyCode: (NSString *)verifyCode
{
BOOL flag;
if (!(verifyCode.length == 6)) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{6})";
NSPredicate *verifyCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [verifyCodePredicate evaluateWithObject:verifyCode];
}
// 類方法自定義正則驗證
+ (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",customRegex];
return [predicate evaluateWithObject:targetString];
}
@end
ViewController
//1.類方法
NSLog(@"我是數(shù)字--類方法-->%d",[SHRegexPatternTool validateBankCardNumber:@"6214830115447426"]);
NSLog(@"我是郵箱--類方法-->%d",[SHRegexPatternTool validateEmail:@"163@qq.com"]);
NSLog(@"我是漢字中文--類方法-->%d",[SHRegexPatternTool validateCustomRegex:@"^[\u4E00-\u9FA5]*$" TargetString:@"我只是中文"]);
//2.實例方法
SHRegexPatternTool *tool = [[SHRegexPatternTool alloc] init];
NSLog(@"我是數(shù)字--實例方法-->%d",[tool validateIsValidQQ:@"1383478"]);
NSLog(@"我是郵箱--實例方法-->%d",[tool validateIsValidMailbox:@"163@qq.com"]);
NSLog(@"我是漢字中文--實例方法-->%d",[tool validateCustomRegex:@"^[\u4E00-\u9FA5]*$" TargetString:@"我只是中文"]);