手機號、郵箱、身份證的合法性

1、電話:

//電話號碼

- (BOOL)checkTel:(NSString *)mobileNumbel

{

/**

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

* 聯通:130,131,132,152,155,156,185,186

* 電信:133,1349,153,180,189,181(增加)

*/

NSString * MOBIL = @"^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[2378])\\d)\\d{7}$";

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

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

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

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

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

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

if (([regextestmobile evaluateWithObject:mobileNumbel]

|| [regextestcm evaluateWithObject:mobileNumbel]

|| [regextestct evaluateWithObject:mobileNumbel]

|| [regextestcu evaluateWithObject:mobileNumbel])) {

NSLog(@"手機號驗證可用");

return YES;

}

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請輸入正確的手機號碼" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

return NO;

}

2、郵箱:

-(BOOL)validateEmail:(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(@"恭喜!您輸入的郵箱驗證合法");

return YES;

}else{

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請輸入正確的郵箱" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

return NO;

}

return NO;

}

3、身份證:

- (BOOL)checkIdentityCardNo:(NSString*)cardNo

{

if (cardNo.length != 18) {

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"對不起!省份證的位數不夠或過多" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

return? NO;

}

NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil];

NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]? forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];

NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];

int val;

BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];

if (!isNum) {

NSLog(@"輸入的省份證號碼不對");

return NO;

}

int sumValue = 0;

for (int i =0; i<17; i++) {

sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];

}

NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];

if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {

NSLog(@"驗證省份證號碼可用");

return YES;

}

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"省份證份證號碼錯誤" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

return? NO;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容