1. 程式人生 > >驗證手機號/郵箱是否合法

驗證手機號/郵箱是否合法

object form 電信 valid 電話號碼 == -c format hone

//判斷是否是合法的電話號碼

+ (BOOL)isChinaMobile:(NSString *)phoneNum{

BOOL isChinaMobile = NO;

NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";

NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];

if([regextestcm evaluateWithObject:phoneNum] == YES){

isChinaMobile = YES;

// NSLog(@"中國移動");

}

NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";

NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];

if([regextestcu evaluateWithObject:phoneNum] == YES){

isChinaMobile = YES;

// NSLog(@"中國聯通");

}

NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";

NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

if([regextestct evaluateWithObject:phoneNum] == YES){

isChinaMobile = YES;

// NSLog(@"中國電信");

}

return isChinaMobile;

}

//判斷是否是合法的郵箱

+ (BOOL)validateEmail:(NSString *)email{

NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:email];

}

驗證手機號/郵箱是否合法