iOS开发常用校验

整理一些可能会用到的校验方法吧
#define ISNULLSTR(str) (str == nil || (NSObject *)str == [NSNull null] || str.length == 0)
//校验邮箱
+(BOOL) isEmailVerify:(NSString *)email{
    if (ISNULLSTR(email)) {
        return NO;
    }  
    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] == YES);
}
//校验手机号
+(BOOL) isValidateMobile:(NSString *)mobile{
    if (ISNULLSTR(mobile)) {
        return NO;
    }
    //手机号以13, 15,18开头,八个 \d 数字字符
//    NSString *phoneRegex = @"^((147)|(13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
//    NSString *phoneRegex = @"^((147)|(177)|(13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSString * phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    NSLog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:mobile];
}
//校验验证码
+(BOOL)isValidateNumber:(NSString *)password{
    if (ISNULLSTR(password)) {
        return NO;
    }
    NSString *patternString = @"^(?![0-9]{1,16}$)[\\S]{6,16}$";
    NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", patternString];
    return ([regextest evaluateWithObject:password] == YES);
}
//校验密码
+(BOOL)isPasswordVerify:(NSString*) password{
    if(ISNULLSTR(password)) {
        return NO;
    }
//    NSString * patternString = @"^([a-zA-Z0-9_-`~!@#$%^&*()+\\|\\\\=,./?><\\{\\}\\[\\]]{6,18})+$";
    NSString *patternString = @"^[a-zA-Z0-9]{6,16}$";//字母开头由字母和数字组成的6-16位密码
    NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", patternString];
    return ([regextest evaluateWithObject:password] == YES);
}
//验证正整数
+(BOOL)validateIntegerNumber:(NSString *)textString{
    NSString *patternString = @"^[0-9]*$";
    NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",patternString];
    return ([regextest evaluateWithObject:textString] == YES);
}
有遇到其他的话再更新

你可能感兴趣的:(iOS开发常用校验)