您的位置:首页 > 移动开发 > IOS开发

代码总结:IOS正则表达式(项目中用到的)

2014-10-09 15:39 344 查看
#import <Foundation/Foundation.h>

/**
*	@brief	用户输入内容检查
*/
@interface NSString (CheckValid)

/**
*	@brief	检查邮箱名是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidEmail;

/**
*	@brief	检查用户名是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidUserName;

/**
*	@brief	检查管理员账号是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidAdminName;

/**
*	@brief	检查其他名称是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidOtherName;

/**
*	@brief	检查手机号是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidMobileNumber;

/**
*	@brief	检查座机号是否合法
*
*	@return	是否合法
*/
-(BOOL )isValidPhoneNumber;

/**
*	@brief	检查用户账号是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidAccountName;

/**
*	@brief	检查密码是否合法
*
*	@return	是否合法
*/
-(BOOL) isValidPassword;

/**
*	@brief	检查外链设置密码是否合法 // add by qjl 2014-01-21
*
*	@return	是否合法
*/
-(BOOL) isValidlinkSetPassword;

/**
*	@brief	检查是否为纯数字
*
*	@return	是否合法
*/
-(BOOL) isValidNumber;

/**
*	@brief	检查文件名是否有效
*
*	@return	是否有效
*/
- (BOOL)isValidFileName;

@end


#import "NSString+CheckValid.h"

@implementation NSString (CheckValid)

-(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];
}

-(BOOL) isValidUserName
{
NSString *nameRegex = @"^[A-Za-z0-9\\u4e00-\\u9fa5-]{2,20}$";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
return [nameTest evaluateWithObject:self];
}

-(BOOL) isValidAdminName
{
NSString *adminNameRegex = @"^[A-Za-z0-9\\u4e00-\\u9fa5-]{2,20}$";
NSPredicate *adminNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", adminNameRegex];
return [adminNameTest evaluateWithObject:self];
}

-(BOOL) isValidOtherName
{
NSString *otherNameRegex = @"^[A-Za-z0-9\\u4e00-\\u9fa5-]{2,20}$"; //@"^[^ ][\\s\\S]*[^ ]{1,49}$";
NSPredicate *otherNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", otherNameRegex];
return [otherNameTest evaluateWithObject:self];
}

-(BOOL) isValidAccountName
{
NSString *accountNameRegex = @"^[a-zA-Z][a-zA-Z0-9_]{2,20}$";
NSPredicate *accountNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", accountNameRegex];
return [accountNameTest evaluateWithObject:self];
}

-(BOOL) isValidPassword
{
//NSString *passwordRegex = @"^[a-zA-Z0-9]{6,30}$";  //modified by qjl  15/1/2014 [只能使用英文、数字或两者组合,不能使用特殊符号;长度6-30位]
NSString *passwordRegex = @"^[a-zA-Z0-9-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]{6,20}$"; // 支持特殊字符,modified by LJ  25/11/2013
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
return [passwordTest evaluateWithObject:self];
}

-(BOOL) isValidlinkSetPassword
{
NSString *passwordRegex = @"^[a-zA-Z0-9-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]{4,6}$";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
return [passwordTest evaluateWithObject:self];
}

-(BOOL )isValidMobileNumber
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,183,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189
*/
NSString* MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0-9])\\d{8}$";
/**
* 中国移动:China Mobile
* 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
NSString* CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
* 中国联通:China Unicom
* 130,131,132,152,155,156,185,186
*/
NSString* CU= @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
* 中国电信:China Telecom
* 133,1349,153,180,189
*/
NSString* CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
* 大陆地区固话及小灵通
* 区号:010,020,021,022,023,024,025,027,028,029 ...
* 号码:七位或八位
*/

//只判断位数 7到12位
// NSString * PHS = @"^[0-9]{6,20}$";

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];
//NSPredicate*regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",PHS];

if(([regextestmobile evaluateWithObject:self] == YES)
|| ([regextestcm evaluateWithObject:self] == YES)
|| ([regextestct evaluateWithObject:self] == YES)
|| ([regextestcu evaluateWithObject:self] == YES))
{
return YES;
}
else
{
return NO;
}
}

-(BOOL )isValidPhoneNumber
{
//只判断位数 4到12位
NSString * PHS = @"^[0-9]{4,12}$";

NSPredicate *regextestphone =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",PHS];
if([regextestphone evaluateWithObject:self] == YES)
return YES;
else
return NO;
}

- (BOOL)isValidNumber
{
NSString *numberRegex = @"^[0-9]+$";
NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
return [numberTest evaluateWithObject:self];
}

- (BOOL)isValidFileName
{
//判断文件名是否合法
NSString *fileNameRegex = @"^[a-zA-Z0-9\\u4e00-\\u9fa5-`=\\[\\];',.~!@#$%^&()_+{}\\s*]+$";
NSPredicate *fileNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", fileNameRegex];
return [fileNameTest evaluateWithObject:self];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: