#import "ViewController.h"
#import "ZYPredicateTool.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *IDTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//? 例子
- (IBAction)testName:(id)sender {
if ([ZYPredicateTool validateTrueName:self.IDTextField.text] )
{
NSLog(@"是姓名");
}else{
NSLog(@"不是姓名");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上是調(diào)用
-------------華麗的分割線---------------
.h 文件
#import@interface ZYPredicateTool : NSObject
//? 檢驗(yàn)密碼羹唠,6~20位末购,輸入(字母或者數(shù)字)
+ (BOOL)validatePassword:(NSString *)passWord;
//? 檢驗(yàn)是否為用戶名
+ (BOOL)validateTrueName:(NSString *)trueName;
//? 檢驗(yàn)是否是身份證號(hào)碼
+ (BOOL) validateIdentityCard: (NSString *)identityCard;
// 郵箱
+ (BOOL) validateEmail:(NSString *)email;
// 手機(jī)號(hào)碼驗(yàn)證
+ (BOOL) validateMobile:(NSString *)mobile;
//? 車牌號(hào)驗(yàn)證
+ (BOOL) validateCarNo:(NSString *)carNo;
//? 車型
+ (BOOL) validateCarType:(NSString *)CarType;
//? 昵稱
+ (BOOL) validateNickname:(NSString *)nickname;
//? url
+ (BOOL) validateURL: (NSString *)url;
@end
.m 文件
#import "ZYPredicateTool.h"
@implementation ZYPredicateTool
//? 檢驗(yàn)密碼,6~20位臭墨,輸入(字母或者數(shù)字)
+ (BOOL)validatePassword:(NSString *)passWord
{
NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicate evaluateWithObject:passWord];
}
//? 檢驗(yàn)是否為用戶名
+ (BOOL)validateTrueName:(NSString *)trueName
{
NSString *regex2 = @"^[\u4e00-\u9fa5]{2,20}";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:trueName];
}
//? 檢驗(yàn)是否是身份證號(hào)碼
+ (BOOL)validateIdentityCard:(NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}[xX0-9]{1}$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
}
//? 郵箱
+ (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];
}
//? 手機(jī)號(hào)碼驗(yàn)證
+ (BOOL) validateMobile:(NSString *)mobile
{
//手機(jī)號(hào)以13, 15狞悲,18火欧,17 開頭,八個(gè) \d 數(shù)字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//? 車牌號(hào)驗(yàn)證
+ (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]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
BOOL B = [userNamePredicate evaluateWithObject:name];
return B;
}
//? 昵稱
+ (BOOL) validateNickname:(NSString *)nickname
{
NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [passWordPredicate evaluateWithObject:nickname];
}
//? url
+ (BOOL) validateURL: (NSString *)url
{
NSString *regex = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
NSPredicate *urlPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [urlPredicate evaluateWithObject:url];
}
@end