您的位置:首页 > 移动开发 > IOS开发

iOS邮箱、手机号等常用验证功能

2016-12-11 11:54 387 查看
手机号验证
/*
手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9]
移动:134[0-8],13[5-9],147,15[0-2],15[7-9],178,18[2-4],18[7-8]
联通:13[0-2],145,15[5-6],17[5-6],18[5-6]
电信:133,1349,149,153,173,177,180,181,189
虚拟运营商: 170[0-2]电信  170[3|5|6]移动 170[4|7|8|9],171 联通
上网卡又称数据卡,14号段为上网卡专属号段,中国联通上网卡号段为145,中国移动上网卡号段为147,中国电信上网卡号段为149
*/
-(BOOL)isMobilePhone:(NSString *)phoneNum
{
NSString * MOBIL = @"^1(3[0-9]|4[579]|5[0-35-9]|7[01356]|8[0-9])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
if ([regextestmobile evaluateWithObject:phoneNum]) {
return YES;
}
return NO;
}
是否是移动号码
/**
* 中国移动:China Mobile
* 134[0-8],13[5-9],147,15[0-2],15[7-9],170[3|5|6],178,18[2-4],18[7-8]
*/
-(BOOL)isCMMobilePhone:(NSString *)phoneNum
{
NSString * CM = @"^1(34[0-8]|70[356]|(3[5-9]|4[7]|5[0-27-9]|7[8]|8[2-47-8])\\d)\\d{7}$";
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
if ([regextestcm evaluateWithObject:phoneNum]) {
return YES;
}
return NO;
}
是否是联通号码
/**
* 中国联通:China Unicom
* 13[0-2],145,15[5-6],17[5-6],18[5-6],170[4|7|8|9],171
*/
-(BOOL)isCUMobilePhone:(NSString *)phoneNum
{
NSString * CU = @"^1(70[07-9]|(3[0-2]|4[5]|5[5-6]|7[15-6]|8[5-6])\\d)\\d{7}$";
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
if ([regextestcu evaluateWithObject:phoneNum]) {
return YES;
}
return NO;
}
是否是电信号码
/**
* 中国电信:China Telecom
* 133,1349,149,153,173,177,180,181,189,170[0-2]
*/
-(BOOL)isCTMobilePhone:(NSString *)phoneNum
{
NSString * CT = @"^1(34[9]|70[0-2]|(3[3]|4[9]|5[3]|7[37]|8[019])\\d)\\d{7}$";
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if ([regextestct evaluateWithObject:phoneNum]) {
return YES;
}
return NO;
}

邮箱验证

- (void)validationEmail:(NSString *)email {

NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if( [emailTest evaluateWithObject:email]){
NSLog(@"恭喜!您输入的邮箱验证合法");
}else{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的邮箱" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];

}
}

将手机号中特定位置替换为*号

- (NSString *)replaceStringWithAsterisk:(NSInteger)startLocation length:(NSInteger)length {
NSString *replaceStr = self;
for (NSInteger i = 0; i < length; i++) {
NSRange range = NSMakeRange(startLocation, 1);
replaceStr = [replaceStr stringByReplacingCharactersInRange:range withString:@"*"];
startLocation ++;
}
return replaceStr;
}

6~16位数字、字母、下划线组成的密码格式校验
-(BOOL)checkPsw:(NSString *)pswStr
{
NSString * pattern  =   @"^[A-Za-z0-9_]{6,16}$";
//    NSString *condition =@"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,16}";
NSPredicate * pred  =   [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
return [pred evaluateWithObject:pswStr];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: