ios 推断是qq,银行卡,手机号等等公用的方法。
#import <Foundation/Foundation.h>
typedef enum
{
IdentifierTypeKnown = 0,
IdentifierTypeZipCode, //1
IdentifierTypeEmail, //2
IdentifierTypePhone, //3
IdentifierTypeUnicomPhone, //4
IdentifierTypeQQ, //5
IdentifierTypeNumber, //6
IdentifierTypeString, //7
IdentifierTypeIdentifier, //8
IdentifierTypePassort, //9
IdentifierTypeCreditNumber, //10
IdentifierTypeBirthday, //11
}IdentifierType;
@interface IdentifierValidator : NSObject
{
}
+ (BOOL) isValid:(IdentifierType) type value:(NSString*) value;
@end
#import "IdentifierValidator.h"
#import "NSString+ITTAdditions.h"
int getIndex (char ch);
BOOL isNumber (char ch);
int getIndex (char ch) {
if ((ch >= '0'&& ch <= '9')||(ch >= 'a'&& ch <= 'z')||
(ch >= 'A' && ch <= 'Z')|| ch == '_') {
return 0;
}
if (ch == '@') {
return 1;
}
if (ch == '.') {
return 2;
}
return -1;
}
BOOL isNumber (char ch)
{
if (!(ch >= '0' && ch <= '9')) {
return FALSE;
}
return TRUE;
}
@implementation IdentifierValidator
+ (BOOL) isValidZipcode:(NSString*)value
{
const char *cvalue = [value UTF8String];
int len = strlen(cvalue);
if (len != 6) {
return FALSE;
}
for (int i = 0; i < len; i++)
{
if (!(cvalue[i] >= '0' && cvalue[i] <= '9'))
{
return FALSE;
}
}
return TRUE;
}
+ (BOOL) validateEmail:(NSString *)candidate
{
NSArray *array = [candidate componentsSeparatedByString:@"."];
if ([array count] >= 4) {
return FALSE;
}
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:candidate];
}
+ (BOOL) isValidEmail:(NSString*)value {
static int state[5][3] = {
{1, -1, -1},
{1, 2, -1},
{3, -1, -1},
{3, -1, 4},
{4, -1, -1}
};
BOOL valid = TRUE;
const char *cvalue = [value UTF8String];
int currentState = 0;
int len = strlen(cvalue);
int index;
for (int i = 0; i < len && valid; i++) {
index = getIndex(cvalue[i]);
if (index < 0) {
valid = FALSE;
}
else {
currentState = state[currentState][index];
if (currentState < 0) {
valid = FALSE;
}
}
}
//end state is invalid
if (currentState != 4) {
valid = FALSE;
}
return valid;
}
+ (BOOL) isValidNumber:(NSString*)value{
const char *cvalue = [value UTF8String];
int len = strlen(cvalue);
for (int i = 0; i < len; i++) {
if(!isNumber(cvalue[i])){
return FALSE;
}
}
return TRUE;
}
+ (BOOL) isValidPhone:(NSString*)value {
const char *cvalue = [value UTF8String];
int len = strlen(cvalue);
if (len != 11) {
return FALSE;
}
if (![IdentifierValidator isValidNumber:value])
{
return FALSE;
}
NSString *preString = [[NSString stringWithFormat:@"%@",value] substringToIndex:2];
if ([preString isEqualToString:@"13"] ||
[preString isEqualToString: @"15"] ||
[preString isEqualToString: @"18"])
{
return TRUE;
}
else
{
return FALSE;
}
return TRUE;
}
+ (BOOL) isValidString:(NSString*)value
{
return value && [value length];
}
const int factor[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };//加权因子
const int checktable[] = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };//校验值相应表
+ (BOOL) isValidIdentifier:(NSString*)value
{
const int LENGTH = 18;
const char *str = [[value lowercaseString] UTF8String];
NSInteger i;
NSInteger length = strlen(str);
BOOL result = TRUE;
/*
* identifier length is invalid
*/
if (15 != length && LENGTH != length)
{
result = FALSE;
}
else
{
for (i = 1; i < length - 1; i++)
{
if(!(str[i] >= '0' && str[i] <= '9'))
{
result = FALSE;
break;
}
}
if (result)
{
if(LENGTH == length)
{
if (!((str[i] >= '0' && str[i] <= '9')||str[i] == 'X'||str[i] == 'x'))
{
result = FALSE;
}
}
}
/*
* check sum for second generation identifier
*/
if (result && length == LENGTH)
{
int i;
int *ids = malloc(sizeof(int)*LENGTH);
for (i = 0; i < LENGTH; i++)
{
ids[i] = str[i] - 48;
}
int checksum = 0;
for (i = 0; i < LENGTH - 1; i ++ )
{
checksum += ids[i] * factor[i];
}
if (ids[17] == checktable[checksum%11]||
(str[17] == 'x' && checktable[checksum % 11] == 10))
{
result = TRUE;
}
else
{
result = FALSE;
}
free(ids);
ids = NULL;
}
}
return result;
}
+ (BOOL) isValidPassport:(NSString*)value
{
const char *str = [value UTF8String];
char first = str[0];
NSInteger length = strlen(str);
if (!(first == 'P' || first == 'G'))
{
return FALSE;
}
if (first == 'P')
{
if (length != 8)
{
return FALSE;
}
}
if (first == 'G')
{
if (length != 9)
{
return FALSE;
}
}
BOOL result = TRUE;
for (NSInteger i = 1; i < length; i++)
{
if (!(str[i] >= '0' && str[i] <= '9'))
{
result = FALSE;
break;
}
}
return result;
}
/*
* 经常使用信用卡卡号规则
* Issuer Identifier Card Number Length
* Diner's Club 300xxx-305xxx, 3095xx, 36xxxx, 38xxxx 14
* American Express 34xxxx, 37xxxx 15
* VISA 4xxxxx 13, 16
* MasterCard 51xxxx-55xxxx 16
* JCB 3528xx-358xxx 16
* Discover 6011xx 16
* 银联 622126-622925 16
*
* 信用卡号验证基本算法:
* 偶数位卡号奇数位上数字*2。奇数位卡号偶数位上数字*2。
* 大于10的位数减9。
* 所有数字加起来。
* 结果不是10的倍数的卡号非法。
* prefrences link:http://www.truevue.org/licai/credit-card-no
*
*/
+ (BOOL) isValidCreditNumber:(NSString*)value
{
BOOL result = TRUE;
NSInteger length = [value length];
if (length >= 13)
{
result = [IdentifierValidator isValidNumber:value];
if (result)
{
NSInteger twoDigitBeginValue = [[value substringWithRange:NSMakeRange(0, 2)] integerValue];
NSInteger threeDigitBeginValue = [[value substringWithRange:NSMakeRange(0, 3)] integerValue];
NSInteger fourDigitBeginValue = [[value substringWithRange:NSMakeRange(0, 4)] integerValue];
//Diner's Club
if (((threeDigitBeginValue >= 300 && threeDigitBeginValue <= 305)||
fourDigitBeginValue == 3095||twoDigitBeginValue==36||twoDigitBeginValue==38) && (14 != length))
{
result = FALSE;
}
//VISA
else if([value isStartWithString:@"4"] && !(13 == length||16 == length))
{
result = FALSE;
}
//MasterCard
else if((twoDigitBeginValue >= 51||twoDigitBeginValue <= 55) && (16 != length))
{
result = FALSE;
}
//American Express
else if(([value isStartWithString:@"34"]||[value isStartWithString:@"37"]) && (15 != length))
{
result = FALSE;
}
//Discover
else if([value isStartWithString:@"6011"] && (16 != length))
{
result = FALSE;
}
else
{
NSInteger begin = [[value substringWithRange:NSMakeRange(0, 6)] integerValue];
//CUP
if ((begin >= 622126 && begin <= 622925) && (16 != length))
{
result = FALSE;
}
//other
else
{
result = TRUE;
}
}
}
if (result)
{
NSInteger digitValue;
NSInteger checkSum = 0;
NSInteger index = 0;
NSInteger leftIndex;
//even length, odd index
if (0 == length%2)
{
index = 0;
leftIndex = 1;
}
//odd length, even index
else
{
index = 1;
leftIndex = 0;
}
while (index < length)
{
digitValue = [[value substringWithRange:NSMakeRange(index, 1)] integerValue];
digitValue = digitValue*2;
if (digitValue >= 10)
{
checkSum += digitValue/10 + digitValue%10;
}
else
{
checkSum += digitValue;
}
digitValue = [[value substringWithRange:NSMakeRange(leftIndex, 1)] integerValue];
checkSum += digitValue;
index += 2;
leftIndex += 2;
}
result = (0 == checkSum%10) ? TRUE:FALSE;
}
}
else
{
result = FALSE;
}
return result;
}
+ (BOOL) isValidBirthday:(NSString*)birthday
{
BOOL result = FALSE;
if (birthday && 8 == [birthday length])
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:birthday];
[formatter release];
if (date)
{
result = TRUE;
}
}
return result;
}
+ (BOOL) isChinaUnicomPhoneNumber:(NSString*) phonenumber
{
/**
* 手机号码
* 移动: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 * 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,182,187,188
12 */
// NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\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}$";
// NSString * PHS1 = @"^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];
// NSPredicate *regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
// NSPredicate *regextestphs1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS1];
if (//([regextestmobile evaluateWithObject:phonenumber] == YES)||
// ([regextestcm evaluateWithObject:phonenumber] == YES)||
// ([regextestct evaluateWithObject:phonenumber] == YES)||
([regextestcu evaluateWithObject:phonenumber] == YES)
// || ([regextestphs evaluateWithObject:phonenumber] == YES)
// || ([regextestphs1 evaluateWithObject:phonenumber] == YES)
)
{
return YES;
}
else
{
return NO;
}
}
//+ (BOOL) isChinaUnicomPhoneNumber:(NSString*) phoneNumber
//{
// BOOL unicom = TRUE;
// NSString *mobileNumFormat13 = @"[1]{1}[3]{1}[4-9]{1}[0-9]{8}";
// NSString *mobileNumFormat14 = @"[1]{1}[4]{1}[7]{1}[0-9]{8}";
// NSString *mobileNumFormat15 = @"[1]{1}[5]{1}[012789]{1}[0-9]{8}";
// NSString *mobileNumFormat18 = @"[1]{1}[8]{1}[2378]{1}[0-9]{8}";
// NSPredicate *predicate13 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat13];
// NSPredicate *predicate14 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat14];
// NSPredicate *predicate15 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat15];
// NSPredicate *predicate18 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat18];
// if ([predicate13 evaluateWithObject:phoneNumber] ||
// [predicate14 evaluateWithObject:phoneNumber] ||
// [predicate15 evaluateWithObject:phoneNumber] ||
// [predicate18 evaluateWithObject:phoneNumber])
// {
// unicom = FALSE;
// }
// return unicom;
//}
+ (BOOL) isValid:(IdentifierType) type value:(NSString*) value
{
if (!value ||[[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]) {
return FALSE;
}
BOOL result = TRUE;
switch (type)
{
case IdentifierTypeZipCode:
result = [IdentifierValidator isValidZipcode:value];
break;
case IdentifierTypeEmail:
// result = [IdentifierValidator isValidEmail:value];
result = [IdentifierValidator validateEmail:value];
break;
case IdentifierTypePhone:
result = [IdentifierValidator isValidPhone:value];
break;
case IdentifierTypeUnicomPhone:
result = [IdentifierValidator isChinaUnicomPhoneNumber:value];
break;
case IdentifierTypeQQ:
result = [IdentifierValidator isValidNumber:value];
break;
case IdentifierTypeNumber:
result = [IdentifierValidator isValidNumber:value];
break;
case IdentifierTypeString:
result = [IdentifierValidator isValidString:value];
break;
case IdentifierTypeIdentifier:
result = [IdentifierValidator isValidIdentifier:value];
break;
case IdentifierTypePassort:
result = [IdentifierValidator isValidPassport:value];
break;
case IdentifierTypeCreditNumber:
result = [IdentifierValidator isValidCreditNumber:value];
break;
case IdentifierTypeBirthday:
result = [IdentifierValidator isValidBirthday:value];
break;
default:
break;
}
return result;
}
- (void) dealloc
{
[super dealloc];
}
@end
ios 推断是qq,银行卡,手机号等等公用的方法。的更多相关文章
- 微信、QQ和手机号之间不得不说的故事!
发文字,发图片,发心情,视频聊天,查看附近的人,微信能干的事情QQ都可以,那么它们有什么区别,我QQ用得好好的为什么要我联系人都导到微信去?我们很早就有了QQ,但是在QQ时代,我们虽然用QQ发消息聊天 ...
- iOS打开手机QQ与指定用户聊天界面
开发中遇到一个联系客服qq的需求,找到这么一个实现方法,先记录下来.大概的原理就是,iOS启动第三方应用是采用schema模式的,这有点像url,打开不同的界面使用不同的地址.但这个url怎么得来的还 ...
- php通过gbk编码判断 含有连续数字 可用于判断QQ号,手机号等。
有可能有些输入,不希望让用户的评论或者私信中含有类似于QQ号,手机号的文本,比如交友网站.还有些恶意SEO通过,构造恶意检索词,检索词中包含QQ,手机号等,让百度爬取到,增加展现.也需要将这些检索词屏 ...
- Android仿QQ ios dialog,仿QQ退出向上菜单
Android仿QQ ios dialog,仿QQ退出向上菜单 EasyDialog两种模式 仿QQ退出向上菜单,自己定义向上菜单 github地址:https://gith ...
- iOS之处理不等高TableViewCell的几种方法
课题一:如何计算Cell高度 方案一:直接法(面向对象) 直接法,就是把数据布局到Cell上,然后拿到Cell最底部控件的MaxY值. 第一步:创建Cell并正确设置约束,使文字区域高度能够根据文字内 ...
- IOS中获取各种文件的路径介绍及方法
IOS中获取各种文件的目录路径的方法 技术交流新QQ群:414971585 iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. docum ...
- iOS阶段学习第32天笔记(页面传值方法介绍)
iOS学习(UI)知识点整理 一.界面传值方法 1.方法一 Block传值 通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码: 1)SubViewContro ...
- 让ecshop用户名、手机号、email登陆方法
让ecshop用户名.手机号.email登陆方法, 仅适用于没有做过任何平台整合的ECSHOP网站 修改文件: 1.includes/modules/integrates/ecshop.php ...
- 关于iOS去除数组中重复数据的几种方法
关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...
随机推荐
- 使用form-create动态生成vue组件
使用form-create动态生成vue自定义组件和嵌套表单组件 [github] | [说明文档] 示例 let rule = [ { type:'row', children:[ { type:' ...
- java实现折半查找
package althorgrim;/** * 1.必须采用顺序存储结果 * 2.关键字必须有序 * @author hanrk-2734 * */public class TestBinarySe ...
- javascript 将时间戳格式化
<script>function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().repl ...
- POJ 2323 贪心
题意: 思路: 贪 贪 贪 如果当前的c>之前的c+s 那么之前的合适 一直贪下去就好了 //By SiriusRen #include <cstdio> #include < ...
- OpenCV —— 直方图与匹配
直方图就是对数据进行统计,将统计值组织到一系列事先定义好的bin中.bin中的数值是从数据中计算出来的特征的统计量,这些数据可以是诸如梯度,方向,色彩或任何其他特征. 直方图获得是是数据分布的统计图 ...
- 2017国家集训队作业[agc014d]Black and White Tree
2017国家集训队作业[agc014d]Black and White Tree 题意: 有一颗n个点的树,刚开始每个点都没有颜色.Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,B ...
- 解决Docker容器内访问宿主机MySQL数据库服务器的问题
懒得描述太多,总归是解决了问题,方法简要记录如下,虽然简要,但是完整,一来纪念处理该问题耗费的大半天时间,二来本着共享精神帮助其他遇到该问题的哥们儿,当然这个方法并不一定能解决你们的问题,但是多少能提 ...
- Nio学习3——基础模型:多路复用模型
Reactor模式和NIO 本文可看成是对Doug Lea Scalable IO in Java一文的翻译. 当前分布式计算 Web Services盛行天下,这些网络服务的底层都离不开对socke ...
- HDOJ 5399 Too Simple
每个函数都必须是一个排列,经过连续的一段确定函数后数字不能少. 满足上面的条件的话,仅仅要有一个-1函数特别的排列一下就能够满足要求,剩下的能够任意填 没有-1的话特判 Too Simple Time ...
- 关于大数据项目创建时所需setting.xml(博主推荐)
我目前,收录经常用的是,这两个版本,这个根据博主我本人的经验之谈,最为稳定和合理的. 注意:我的本地路径是在D:/SoftWare/maven/repository,大家自己改为你们自己的即可. ...