iOS正則判斷

weixin_33806914發表於2017-01-09

#pragma 正則判斷登入密碼是否正確(4-8位數字字母結合)

- (BOOL)validateNumber:(NSString *) textString

{

NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,8}$";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

return  [pred evaluateWithObject:textString];

}


#pragma mark 判斷手機號是否正確

- (BOOL)chectTel:(NSString *)phoneStr {

/**

* 手機號碼

* 移動: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

*/

NSString *phone=@"^1([0-9][0-9])\\d{8}$";

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

/**

10 * 中國移動:China Mobile

11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,181,184,182,187,188

12 */

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

/**

15 * 中國聯通:China Unicom

16 * 130,131,132,152,155,156,185,186

17 */

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

/**

20 * 中國電信:China Telecom

21 * 133,1349,153,180,189

22 */

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

/**

25 * 大陸地區固話及小靈通

26 * 區號:010,020,021,022,023,024,025,027,028,029

27 * 號碼:七位或八位

28 */

// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

NSPredicate *regextestphone=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",phone];

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:_phoneTextField.text] == YES)

|| ([regextestcm evaluateWithObject:_phoneTextField.text] == YES)

|| ([regextestct evaluateWithObject:_phoneTextField.text] == YES)

|| ([regextestcu evaluateWithObject:_phoneTextField.text] == YES)||([regextestphone evaluateWithObject:_phoneTextField.text]==YES))

{

return YES;

}

else

{

return NO;

}

}

#pragma mark - 檢查身份證號碼

+ (BOOL)judgeIdentityStringValid:(NSString *)identityString {

if (identityString.length != 18) return NO;

// 正規表示式判斷基本 身份證號是否滿足格式

NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";

NSPredicate *identityStringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

//如果通過該驗證,說明身份證格式正確,但準確性還需計算

if(![identityStringPredicate evaluateWithObject:identityString]) return NO;

//** 開始進行校驗 *//

//將前17位加權因子儲存在陣列裡

NSArray *idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];

//這是除以11後,可能產生的11位餘數、驗證碼,也儲存成陣列

NSArray *idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];

//用來儲存前17位各自乖以加權因子後的總和

NSInteger idCardWiSum = 0;

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

NSInteger subStrIndex = [[identityString substringWithRange:NSMakeRange(i, 1)] integerValue];

NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];

idCardWiSum+= subStrIndex * idCardWiIndex;

}

//計算出校驗碼所在陣列的位置

NSInteger idCardMod=idCardWiSum%11;

//得到最後一位身份證號碼

NSString *idCardLast= [identityString substringWithRange:NSMakeRange(17, 1)];

//如果等於2,則說明校驗碼是10,身份證號碼最後一位應該是X

if(idCardMod==2) {

if(![idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]) {

return NO;

}

}

else{

//用計算出的驗證碼與最後一位身份證號碼匹配,如果一致,說明通過,否則是無效的身份證號碼

if(![idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]) {

return NO;

}

}

return YES;

}

相關文章