判断字符是否有中文
//判断是否有中文
-(BOOL)IsChinese:(NSString *)str
{
if ([str length] > 1)
{
for (int i=0; i< [str length];i++)
{
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff)
{
return YES;
}
}
}
return NO;
}
判断邮箱号
//如果符合邮箱格式,返回YES
-(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];
}
判断密码为6~20位字符
- (BOOL)isValidatePassword:(NSString *)password{
NSString *regex = @"^[\x21-\x7E]{6,20}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:password];
}
判断手机格式
+ (BOOL)checkTel:(NSString *)str{
NSString *regex = @"^[1][3-8]+\\d{9}";//手机号码变化大,所以没有做更精确
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:str];;
}
判断邮编格式为6位数字
+ (BOOL)checkPostCode:(NSString *)str{
NSString *regex = @"^[0-9]{6}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:str];
}
随机获取英文字母
//根据传入的长度相应的生成多少位小写字母的随机字符
+ (NSString *)getRandomStringWithCharNum:(NSInteger)charNum
{
@autoreleasepool {
NSString *randomStr = nil;
char data[charNum];
for (int x=0;x<charNum;data[x++] = (char)('a' + (arc4random_uniform(26))));
randomStr = [[NSString alloc] initWithBytes:data length:charNum encoding:NSUTF8StringEncoding];
return randomStr;
}
}
//根据传入的长度相应的生成多少位大写字母的随机字符
+ (NSString *)getRandomStringWithCharNum:(NSInteger)charNum
{
@autoreleasepool {
NSString *randomStr = nil;
char data[charNum];
for (int x=0;x<charNum;data[x++] = (char)('A' + (arc4random_uniform(26))));
randomStr = [[NSString alloc] initWithBytes:data length:charNum encoding:NSUTF8StringEncoding];
return randomStr;
}
}