1、郵箱
//郵箱
- (BOOL) isEmail
{
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:self];
}
2卦睹、手機(jī)號(hào)
///// 手機(jī)號(hào)碼的有效性判斷
//檢測(cè)是否是固話
- (BOOL)isFaxNumber
{
/**
* 手機(jī)號(hào)碼
* 移動(dòng):134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 聯(lián)通:130,131,132,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}$";
/**
10 * 中國(guó)移動(dòng):China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
// NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
15 * 中國(guó)聯(lián)通: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 * 中國(guó)電信:China Telecom
21 * 133,1349,153,180,189
22 */
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
25 * 大陸地區(qū)固話及小靈通
26 * 區(qū)號(hào):010,020,021,022,023,024,025,027,028,029
27 * 號(hào)碼:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
// 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:mobileNum] == YES)
// || ([regextestcm evaluateWithObject:mobileNum] == YES)
// || ([regextestct evaluateWithObject:mobileNum] == YES)
// || ([regextestcu evaluateWithObject:mobileNum] == YES))
if ([regextestct evaluateWithObject: self] == YES)
{
return YES;
}
else
{
return NO;
}
}
///// 手機(jī)號(hào)碼的有效性判斷
- (BOOL)isMobileNumber
{
/**
* 手機(jī)號(hào)碼
* 移動(dòng):134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 聯(lián)通:130,131,132,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}$";
/**
10 * 中國(guó)移動(dòng):China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
15 * 中國(guó)聯(lián)通: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 * 中國(guó)電信:China Telecom
21 * 133,1349,153,180,189
22 */
// NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
25 * 大陸地區(qū)固話及小靈通
26 * 區(qū)號(hào):010,020,021,022,023,024,025,027,028,029
27 * 號(hào)碼:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
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:self] == YES)
|| ([regextestcm evaluateWithObject:self] == YES)
// || ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:self] == YES))
{
return YES;
}
else
{
return NO;
}
}
3畦戒、身份證
//判斷身份證號(hào)碼
- (BOOL) isIdCardNo{
BOOL flag;
if (self.length <= 0)
{
flag = NO;
return flag;
}
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 *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
flag = [identityCardPredicate evaluateWithObject:self];
//如果通過(guò)該驗(yàn)證,說(shuō)明身份證格式正確结序,但準(zhǔn)確性還需計(jì)算
if(flag)
{
if(self.length==18)
{
//將前17位加權(quán)因子保存在數(shù)組里
NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
//這是除以11后障斋,可能產(chǎn)生的11位余數(shù)、驗(yàn)證碼徐鹤,也保存成數(shù)組
NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
//用來(lái)保存前17位各自乖以加權(quán)因子后的總和
NSInteger idCardWiSum = 0;
for(int i = 0;i < 17;i++)
{
NSInteger subStrIndex = [[self substringWithRange:NSMakeRange(i, 1)] integerValue];
NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
idCardWiSum+= subStrIndex * idCardWiIndex;
}
//計(jì)算出校驗(yàn)碼所在數(shù)組的位置
NSInteger idCardMod=idCardWiSum%11;
//得到最后一位身份證號(hào)碼
NSString * idCardLast= [self substringWithRange:NSMakeRange(17, 1)];
//如果等于2垃环,則說(shuō)明校驗(yàn)碼是10,身份證號(hào)碼最后一位應(yīng)該是X
if(idCardMod==2)
{
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
{
return flag;
}else
{
flag = NO;
return flag;
}
}else
{
//用計(jì)算出的驗(yàn)證碼與最后一位身份證號(hào)碼匹配返敬,如果一致遂庄,說(shuō)明通過(guò),否則是無(wú)效的身份證號(hào)碼
if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
{
return flag;
}
else
{
flag = NO;
return flag;
}
}
}
else
{
flag = NO;
return flag;
}
}
else
{
return flag;
}
}
4劲赠、銀行卡
//驗(yàn)證銀行卡號(hào)有效性
- (BOOL)isBankAccount{
int oddsum = 0; //奇數(shù)求和
int evensum = 0; //偶數(shù)求和
int allsum = 0;
int cardNoLength = (int)[self length];
int lastNum = [[self substringFromIndex:cardNoLength-1] intValue];
NSString *cardNo = [self substringToIndex:cardNoLength - 1];
for (int i = cardNoLength -1 ; i>=1;i--) {
NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];
int tmpVal = [tmpString intValue];
if (cardNoLength % 2 ==1 ) {
if((i % 2) == 0){
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}else{
oddsum += tmpVal;
}
}else{
if((i % 2) == 1){
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}else{
oddsum += tmpVal;
}
}
}
allsum = oddsum + evensum;
allsum += lastNum;
if((allsum % 10) == 0)
return YES;
else
return NO;
}