// // FLFormatHelper.m // FirstLink // // Created by unicode on 14-10-9. // Copyright (c) 2014年 FirstLink. All rights reserved. // #import "FLFormatHelper.h" @implementation FLFormatHelper + (BOOL)checkMobileFormat:(NSString *)mobile error:(NSError**)error { if (!mobile || [mobile length] == 0) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"手机号码不能为空"}]; } return NO; } NSString *mobileRegex = @"[1][0-9]{10}"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex]; BOOL matches = [predicate evaluateWithObject:mobile]; if (!matches) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"手机号码格式不正确"}]; } } return matches; } + (BOOL)checkNicknameFormat:(NSString *)nickname error:(NSError **)error { if (!nickname || [nickname length] < 2 || [nickname length] > 12) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"昵称长度为2-12位"}]; } return NO; } if ([nickname rangeOfString:@" "].location != NSNotFound) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"昵称不能含有空格"}]; } return NO; } //一般用4E00-9FA5已经可以,如果要更广,则用2E80-A4CF || F900-FAFF || FE30-FE4F NSString *nicknameRegex = @"^(?!_)(?!.*?_$)[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]{2,12}$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nicknameRegex]; BOOL match = [predicate evaluateWithObject:nickname]; if (!match) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"昵称不合法"}]; } } return match; } + (BOOL)checkPasswordFormat:(NSString *)password error:(NSError *__autoreleasing *)error { if (!password || [password length] == 0) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"密码不能为空"}]; } return NO; } NSString *passwordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex]; BOOL match = [predicate evaluateWithObject:password]; if (!match) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"密码为6-16位且包含数字和字母组合"}]; } } return match; } + (BOOL)checkVerifyCodeFormat:(NSString *)verifyCode error:(NSError**)error { if (!verifyCode || [verifyCode length] == 0) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"验证码不能为空"}]; } return NO; } NSString *verifyCodeRegex = @"[0-9]{6}"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", verifyCodeRegex]; BOOL match = [predicate evaluateWithObject:verifyCode]; if (!match) { if (error != NULL) { *error = [NSError errorWithDomain:@"FirstLinkErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey:@"验证码不正确"}]; } } return match; } @end