首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

开发中 常用正则 判断

开发中 常用正则 判断

.h文件中

#import@interface JKRegular : NSObject

/** 判断是否为手机号码 */

+(BOOL)valiMobileNSString *)mobile;

/** 邮箱 */

+ (BOOL) validateEmailNSString *)email;

/** 用户名 */

+ (BOOL) validateUserNameNSString *)name;

/** 昵称 */

+ (BOOL) validateNicknameNSString *)nickname;

/** 身份证 */

+ (BOOL) validateIdentityCard: (NSString *)identityCard;

/** 判断是否为纯数字 */

+(BOOL)isPureNumandCharactersNSString *)string;

@end

.m文件

@implementation JKRegular

+(BOOL)valiMobileNSString *)mobile{

/**

* 移动号段正则表达式

*/

NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";

/**

* 联通号段正则表达式

*/

NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";

/**

* 电信号段正则表达式

*/

NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";

/**

*  大陆地区固话及小灵通

*/

NSString *PHS =@"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

NSPredicate *pred1 = [NSPredicate predicateWithFormat"SELF MATCHES %@", CM_NUM];

BOOL isMatch1 = [pred1 evaluateWithObject:mobile];

NSPredicate *pred2 = [NSPredicate predicateWithFormat"SELF MATCHES %@", CU_NUM];

BOOL isMatch2 = [pred2 evaluateWithObject:mobile];

NSPredicate *pred3 = [NSPredicate predicateWithFormat"SELF MATCHES %@", CT_NUM];

BOOL isMatch3 = [pred3 evaluateWithObject:mobile];

NSPredicate *pre4 = [NSPredicate predicateWithFormat"SELF MATCHES %@",PHS];

BOOL isMatch4 = [pre4 evaluateWithObject:mobile];

if (isMatch1 || isMatch2 || isMatch3 ||isMatch4) {

return YES;

}else{

return NO;

}

}

//邮箱

+ (BOOL) validateEmailNSString *)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) validateNicknameNSString *)nickname

{

NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{2,4}$";

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) validateUserNameNSString *)name

{

NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";

NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat"SELF MATCHES %@",userNameRegex];

BOOL B = [userNamePredicate evaluateWithObject:name];

return B;

}

/** 判断是否为纯数字 */

+(BOOL)isPureNumandCharactersNSString *)string

{

string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];

if(string.length > 0)

{

return NO;

}

return YES;

}

@end
返回列表