验证手机号/邮箱是否合法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证手机号/邮箱是否合法相关的知识,希望对你有一定的参考价值。
//判断是否是合法的电话号码
+ (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];
}
以上是关于验证手机号/邮箱是否合法的主要内容,如果未能解决你的问题,请参考以下文章