iOS 开发 几种简单的校验----手机号、密码、URL。。。

判断是否为URL地址

- (BOOL)isTrueUrl {
    NSString *pattern = @"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray *regexArray = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    if (regexArray.count > 0) return YES; else return NO;
}

判断是否为手机号----简单判断

- (BOOL)isPhoneNumber {
    NSString *reg = @"^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1})|(14[4-7]{1}))+\\d{8})$";
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reg];
    return [regextestcm evaluateWithObject:self];
}

判断密码是否输入正确(6-16位)

- (BOOL)cheackPass {
    NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]{6,16}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

手机号中间四位变星

- (NSString *)phoneNumberHiddenCenter {
    if (![self isPhoneNumber]) {
        return nil;
    }
    NSString *startStr = [self substringWithRange:NSMakeRange(0, 3)];
    NSString *endStr = [self substringWithRange:NSMakeRange(self.length-4, 4)];
    NSString *hiddenStr = [NSString stringWithFormat:@"%@****%@",startStr,endStr];
    return hiddenStr;
}

你可能感兴趣的:(iOS 开发 几种简单的校验----手机号、密码、URL。。。)