iOS 字符串邮箱、手机号检测是否合法

平时可以将这些方法添加到自己用的分类中去,更加方便的使用

检查邮箱

#pragma mark -检查邮箱
- (BOOL)judgeEmail
{
    if (((NSString *)self).length ==0) {
        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:self];
}

检查手机号

#pragma mark - 判断手机号是否合法
- (BOOL)judgeTel
{
    if (((NSString *)self).length == 0) {
        return NO;
    }
    
    NSString *regex = @"1\\d{10}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    if (!isMatch) {
        return NO;
    }
    return YES;
    
}

获取字符串的数字

//获取字符串的数字
- (NSString *)getNumber{
    
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    return[[self componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}

 

你可能感兴趣的:(iOS日常问题)