.H
//"VoodaValidataRegex.h"
#import <Foundation/Foundation.h>
@interface VoodaValidataRegex : NSObject
//邮箱
+ (BOOL) validateEmail:(NSString *)email;
//手机号码验证
+ (BOOL) validateMobile:(NSString *)mobile;
//车牌号验证
+ (BOOL) validateCarNo:(NSString *)carNo;
//车型
+ (BOOL) validateCarType:(NSString *)CarType;
//用户名
+ (BOOL) validateUserName:(NSString *)name;
//密码
+ (BOOL) validatePassword:(NSString *)passWord;
//昵称
+ (BOOL) validateNickname:(NSString *)nickname;
//身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard;
@end
.m
#import "VoodaValidataRegex.h"
#import "ValidTool.h"
@implementation VoodaValidataRegex
//邮箱
+ (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 数字字符
// NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
// NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [ValidTool isMobileNumber: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];
DLog(@"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) 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]{4,8}$";
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];
}
// ValidTool.h
//
#import <Foundation/Foundation.h>
@interface ValidTool : NSObject
//验证是否手机号码
+ (BOOL)isMobileNumber:(NSString *)mobileNum;
//验证邮箱合法性
+ (BOOL)isValidateEmail:(NSString *)email;
//获取当前时间簇
+ (NSString *)getNowTimec;
//设置高度
+ (CGFloat)getHeightWithString:(NSString *)str fontsize:(CGFloat)fontsize width:(CGFloat)width;
//设置宽度
+ (CGFloat)getWidthtWithString:(NSString *)str fontsize:(CGFloat)fontsize;
.m
//
// ValidTool.m
#import "ValidTool.h"
@implementation ValidTool
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,183,184,187,188,178
* 联通:130,131,132,152,155,156,185,186,176
* 电信:133,1349,153,180,189,170,177
*/
NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,183,184,187,188,178
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[23478]|78)\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186,176
17 */
NSString * CU = @"^1(3[0-2]|4[57]|5[256]|8[56]|76)\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189,170,177
22 */
NSString * CT = @"^1((33|53|8[09]|7[07])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:mobileNum] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}
}
+ (BOOL)isValidateEmail:(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];
}
//获取当前时间簇
+ (NSString *)getNowTimec
{
NSDate* dat = [NSDate date];
NSTimeInterval a = [dat timeIntervalSince1970]*1000;
NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
return timeString;
}
+ (CGFloat)getWidthtWithString:(NSString *)str fontsize:(CGFloat)fontsize
{
//版本判断
if ([[UIDevice currentDevice] systemVersion].floatValue >= 7.0)
{
/*
第1个参数:最大显示的区域
第2个参数:类型
第3个参数:字典(文字属性)key在NSAttributedString.h(UIKit)
第4个参数:nil
*/
CGRect rect = [str boundingRectWithSize:CGSizeMake(100000, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontsize]} context:nil];
return rect.size.width;
}
else
{
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:fontsize] constrainedToSize:CGSizeMake(100000, 20)];
return size.width;
}
}
+ (CGFloat)getHeightWithString:(NSString *)str fontsize:(CGFloat)fontsize width:(CGFloat)width
{
//版本判断
if ([[UIDevice currentDevice] systemVersion].floatValue >= 7.0)
{
/*
第1个参数:最大显示的区域
第2个参数:类型
第3个参数:字典(文字属性)key在NSAttributedString.h(UIKit)
第4个参数:nil
*/
CGRect rect = [str boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontsize]} context:nil];
return rect.size.height;
}
else
{
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:fontsize] constrainedToSize:CGSizeMake(width, 10000)];
return size.height;
}
}
@end