正则判号断电话号码、身份证号和邮箱地址

前端之家收集整理的这篇文章主要介绍了正则判号断电话号码、身份证号和邮箱地址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


#import <Foundation/Foundation.h>


@interface RegularJudge :NSObject

{

}

+(RegularJudge *)instance;

/***************************************************************

函数名:checkTel:

参数:str//手机号码

功能:正则表达式检查手机号码是否有误

***************************************************************/

- (BOOL)checkTel:(NSString *)str;

函数名:validateEmail

参数 email

功能:检查邮箱是不是符合格式

***************************************************************/


-(BOOL)isValidateEmail:(NSString *)email;

函数名:validateIDCardNumber

参数 value 输入的身份证号码

功能:检验身份证

***************************************************************/


-(BOOL)validateIDCardNumber:(NSString *)value;


@end


#import "RegularJudge.h"


@implementation RegularJudge

static RegularJudge *instance;

+(RegularJudge *)instance

{

@synchronized(self)

{

if (nil==instance)

{

instance=[RegularJudge new];

}

}

return instance;

}

+(id)allocWithZone:(struct _NSZone *)zone

{

@synchronized(self)

{

if (instance==nil)

{

instance=[super allocWithZone:zone];

return instance;

}

}

return nil;

}

***************************************************************/

- (BOOL)checkTel:(NSString *)str

{

if ([str length] ==0)

{

return NO;

}

else

{

//移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188

//联通:130,131,152,155,156,185,186

//电信:133,1349,153,180,189

NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"; //手机号码

NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"; //中国移动

NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$"; //中国联通

NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$"; //电信

NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];

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

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

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

if (([regextestmobile evaluateWithObject:str] == YES) || ([regextestcm evaluateWithObject:str] == YES) || ([regextestct evaluateWithObject:str] == YES)|| ([regextestcu evaluateWithObject:str] == YES))

{

return YES;

}

else

{

return NO;

}

}

}


***************************************************************/


-(BOOL)isValidateEmail:(NSString *)email

{

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:email];

}



***************************************************************/


-(BOOL)validateIDCardNumber:(NSString *)value

{

value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

int length =0;

if (!value) {

return NO;

}else {

length = value.length;

if (length !=15 && length !=18) {

return NO;

}

}

// 省份代码

NSArray *areasArray =@[@"11",@"12",@"13",@"14",@"15",@"21",@"22",@"23",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"41",@"42",@"43",@"44",@"45",@"46",@"50",@"51",@"52",@"53",@"54",@"61",@"62",@"63",@"64",@"65",@"71",@"81",@"82",@"91"];

NSString *valueStart2 = [value substringToIndex:2];

BOOL areaFlag =NO;

for (NSString *areaCode in areasArray) {

if ([areaCode isEqualToString:valueStart2]) {

areaFlag =YES;

break;

}

}

if (!areaFlag) {

return false;

}

NSRegularExpression *regularExpression;

NSUInteger numberofMatch;

int year =0;

switch (length) {

case15:

year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;

if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {

regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"

options:NSRegularExpressionCaseInsensitive

error:nil];//测试出生日期的合法性

}else {

regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"

options:NSRegularExpressionCaseInsensitive

error:nil];//测试出生日期的合法性

}

numberofMatch = [regularExpression numberOfMatchesInString:value

options:NSMatchingReportProgress

range:NSMakeRange(0,value.length)];

// [regularExpression release];

if(numberofMatch >0) {

return YES;

}else {

return NO;

}

case18:

4)].intValue;

if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {

regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"

options:NSRegularExpressionCaseInsensitive

error:nil];// regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"

options:NSRegularExpressionCaseInsensitive

error:nil];// // [regularExpressionrelease];

if(numberofMatch >0) {

int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1+ [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue*3;

int Y = S %11;

NSString *M =@"F";

NSString *JYM =@"10X98765432";

M = [JYM substringWithRange:NSMakeRange(Y,1)];//判断校验位

if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]])

{

return YES;//检测ID的校验位

}else

{

return NO;

}

}

else

{

return NO;

}

default:

return false;

}

}


@end



调用

在要使用判断的.m文件加上

#import "RegularJudge.h"

RegularJudge *regul= [[RegularJudge alloc]init];

if ( ![regul checkTel: self.phoneNo.text]) {

UIAlertView * aleart= [[UIAlertView alloc]initWithTitle:@"提示" message:@"请填写有效的电话号码!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];

[aleart show];

self.phoneNo.text =@"";

}

原文链接:https://www.f2er.com/regex/361619.html

猜你在找的正则表达式相关文章