http://blog.csdn.net/dyllove98/article/details/8635079

IdentifierValidator.h

//

//  IdentifierValidator.h

//  银行卡校验

//

//  Created by 杨梦晓 on 14-9-9.

//  Copyright (c) 2014年 杨梦晓. All rights reserved.

//

#import <Foundation/Foundation.h>

typedefenum

{

IdentifierTypeKnown = ,

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

-----------------------------------------------

IdentifierValidator.m

//

//  IdentifierValidator.m

//  银行卡校验

//

//  Created by 杨梦晓 on 14-9-9.

//  Copyright (c) 2014年 杨梦晓. All rights reserved.

//

#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 == '_') {

;

}

if (ch == '@') {

;

}

if (ch == '.') {

;

}

;

}

BOOL isNumber (char ch)

{

if (!(ch >= '0' && ch <= '9')) {

returnFALSE;

}

returnTRUE;

}

@implementation IdentifierValidator

#pragma mark - 校验邮编

+ (BOOL) isValidZipcode:(NSString*)value

{

const char *cvalue = [value UTF8String];

int len = strlen(cvalue);

) {

returnFALSE;

}

; i < len; i++)

{

if (!(cvalue[i] >= '0' && cvalue[i] <= '9'))

{

return FALSE;

}

}

returnTRUE;

}

#pragma mark - 校验email

+ (BOOL) validateEmail:(NSString *)candidate

{

NSArray *array = [candidate componentsSeparatedByString:@"."];

) {

returnFALSE;

}

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 {

][] = {

{, -, -},

{,  , -},

{, -, -},

{, -, },

{, -, -}

};

BOOL valid = TRUE;

const char *cvalue = [value UTF8String];

;

int len = strlen(cvalue);

int index;

; i < len && valid; i++) {

index = getIndex(cvalue[i]);

) {

valid = FALSE;

}

else {

currentState = state[currentState][index];

) {

valid = FALSE;

}

}

}

//end state is invalid

) {

valid = FALSE;

}

return valid;

}

#pragma mark - 校验数字

+ (BOOL) isValidNumber:(NSString*)value{

const char *cvalue = [value UTF8String];

int len = strlen(cvalue);

; i < len; i++) {

if(!isNumber(cvalue[i])){

return FALSE;

}

}

returnTRUE;

}

#pragma mark - 校验手机号

+ (BOOL) isValidPhone:(NSString*)value {

const char *cvalue = [value UTF8String];

int len = strlen(cvalue);

) {

returnFALSE;

}

if (![IdentifierValidatorisValidNumber:value])

{

returnFALSE;

}

];

if ([preString isEqualToString:@"13"] ||

[preString isEqualToString: @"15"] ||

[preString isEqualToString: @"18"])

{

return TRUE;

}

else

{

returnFALSE;

}

returnTRUE;

}

#pragma mark - 校验字符串

+ (BOOL) isValidString:(NSString*)value

{

return value && [value length];

}

, , , , , , , , , , , , , , , , };//加权因子

, , , , , , , , , , };//校验值对应表

#pragma mark - 校验身份证号

+ (BOOL) isValidIdentifier:(NSString*)value

{

;

const char *str = [[value lowercaseString] UTF8String];

NSInteger i;

NSInteger length = strlen(str);

BOOL result = TRUE;

/*

* identifier length is invalid

*/

!= length && LENGTH != length)

{

result = FALSE;

}

else

{

; i < length - ; 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);

; i < LENGTH; i++)

{

ids[i] = str[i] - ;

}

;

; i < LENGTH - ; i ++ )

{

checksum += ids[i] * factor[i];

}

] == ]||

(str[] == ] == ))

{

result  = TRUE;

}

else

{

result  = FALSE;

}

free(ids);

ids = NULL;

}

}

return result;

}

#pragma mark - 校验护照

+ (BOOL) isValidPassport:(NSString*)value

{

const char *str = [value UTF8String];

];

NSInteger length = strlen(str);

if (!(first == 'P' || first == 'G'))

{

returnFALSE;

}

if (first == 'P')

{

)

{

return FALSE;

}

}

if (first == 'G')

{

)

{

return FALSE;

}

}

BOOL result = TRUE;

; 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

*

*/

#pragma mark - 校验信用卡号

+ (BOOL) isValidCreditNumber:(NSString*)value

{

BOOL result = TRUE;

NSInteger length = [value length];

)

{

result = [IdentifierValidator isValidNumber:value];

if (result)

{

, )] integerValue];

, )] integerValue];

, )] integerValue];

//Diner's Club

&& threeDigitBeginValue <= )||

fourDigitBeginValue == ||twoDigitBeginValue==||twoDigitBeginValue==) && ( != length))

{

result = FALSE;

}

//VISA

== length|| == length))

{

result = FALSE;

}

//MasterCard

||twoDigitBeginValue <= ) && ( != length))

{

result = FALSE;

}

//American Express

!= length))

{

result = FALSE;

}

//Discover

!= length))

{

result = FALSE;

}

else

{

, )] integerValue];

//CUP

&& begin <= ) && ( != length))

{

result = FALSE;

}

//other

else

{

result = TRUE;

}

}

}

if (result)

{

NSInteger digitValue;

;

;

NSInteger leftIndex;

//even length, odd index

== length%)

{

index = ;

leftIndex = ;

}

//odd length, even index

else

{

index = ;

leftIndex = ;

}

while (index < length)

{

digitValue = [[value )] integerValue];

digitValue = digitValue*;

)

{

checkSum += digitValue/ + digitValue%;

}

else

{

checkSum += digitValue;

}

digitValue = [[value )] integerValue];

checkSum += digitValue;

index += ;

leftIndex += ;

}

result = ( == checkSum%) ? TRUE:FALSE;

}

}

else

{

result = FALSE;

}

return result;

}

#pragma mark - 校验生日

+ (BOOL) isValidBirthday:(NSString*)birthday

{

BOOL result = FALSE;

== [birthday length])

{

NSDateFormatter *formatter = [[NSDateFormatteralloc] init];

[formatter setDateFormat:@"yyyyMMdd"];

NSDate *date = [formatter dateFromString:birthday];

if (date)

{

result = TRUE;

}

}

return result;

}

#pragma mark - 校验联通号

+ (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:[NSCharacterSetwhitespaceCharacterSet]] isEqualToString:@""]) {

returnFALSE;

}

BOOL result = TRUE;

switch (type)

{

caseIdentifierTypeZipCode:

result = [IdentifierValidator isValidZipcode:value];

break;

caseIdentifierTypeEmail:

//            result = [IdentifierValidator isValidEmail:value];

result = [IdentifierValidator validateEmail:value];

break;

caseIdentifierTypePhone:

result = [IdentifierValidator isValidPhone:value];

break;

caseIdentifierTypeUnicomPhone:

result = [IdentifierValidator isChinaUnicomPhoneNumber:value];

break;

caseIdentifierTypeQQ:

result = [IdentifierValidator isValidNumber:value];

break;

caseIdentifierTypeNumber:

result = [IdentifierValidator isValidNumber:value];

break;

caseIdentifierTypeString:

result = [IdentifierValidator isValidString:value];

break;

caseIdentifierTypeIdentifier:

result = [IdentifierValidator isValidIdentifier:value];

break;

caseIdentifierTypePassort:

result = [IdentifierValidator isValidPassport:value];

break;

caseIdentifierTypeCreditNumber:

result = [IdentifierValidator isValidCreditNumber:value];

break;

caseIdentifierTypeBirthday:

result = [IdentifierValidator isValidBirthday:value];

break;

default:

break;

}

return result;

}

@end

-----------------------------------------------

NSString+ITTAdditions.h

//

//  NSString+ITTAdditions.h

//  银行卡校验

//

//  Created by 杨梦晓 on 14-9-9.

//  Copyright (c) 2014年 杨梦晓. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface NSString (ITTAdditions)

- (BOOL)isStartWithString:(NSString *)string;

@end

-----------------------------------------------

NSString+ITTAdditions.m

//

//  NSString+ITTAdditions.m

//  银行卡校验

//

//  Created by 杨梦晓 on 14-9-9.

//  Copyright (c) 2014年 杨梦晓. All rights reserved.

//

#import "NSString+ITTAdditions.h"

@implementation NSString (ITTAdditions)

- (BOOL)isStartWithString:(NSString *)string{

int temp = [string intValue];

switch (temp) {

:

return NO;

break;

:

return NO;

break;

:

return NO;

break;

:

return NO;

break;

default:

break;

}

returnYES;

}

@end

-----------------------------------------------

MXViewController.m

//

//  MXViewController.m

//  信用卡校验

//

//  Created by 杨梦晓 on 14-9-9.

//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.

//

#import "MXViewController.h"

#import "IdentifierValidator.h"

@interfaceMXViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutletUITextField *textField;

@end

@implementation MXViewController

- (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)textFieldDidEndEditing:(UITextField *)textField{

// 信用卡校验

if ([IdentifierValidatorisValid:IdentifierTypeCreditNumbervalue:textField.text]) {

NSLog(@"通过");

}else{

NSLog(@"不通过");

}

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[[[UIApplicationsharedApplication] keyWindow] endEditing:YES];

}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSString *text = [textField text];

NSCharacterSet *characterSet = [NSCharacterSetcharacterSetWithCharactersInString:@"0123456789\b"];

string = [string stringByReplacingOccurrencesOfString:@" "withString:@""];

if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {

return NO;

}

text = [text stringByReplacingCharactersInRange:range withString:string];

text = [text stringByReplacingOccurrencesOfString:@" "withString:@""];

NSString *newString = @"";

) {

)];

newString = [newString stringByAppendingString:subString];

) {

newString = [newString stringByAppendingString:@" "];

}

text = [text )];

}

newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];

) {

return NO;

}

[textField setText:newString];

returnNO;

}

- (void)didReceiveMemoryWarning

{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

ios 判断,qq,银行卡,手机号,邮编,生日,数字,字符串,护照, email的更多相关文章

  1. php通过gbk编码判断 含有连续数字 可用于判断QQ号,手机号等。

    有可能有些输入,不希望让用户的评论或者私信中含有类似于QQ号,手机号的文本,比如交友网站.还有些恶意SEO通过,构造恶意检索词,检索词中包含QQ,手机号等,让百度爬取到,增加展现.也需要将这些检索词屏 ...

  2. C#判断字符串是否为数字字符串

    在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现.[方法一]:使用 try{} catch{} 语句.      我们可以在try语句块中试图将str ...

  3. iOS开发之---判断是否是手机号

    iOS开发之---判断是否是手机号

  4. iOS开发,最新判断是否是手机号的正则表达式

    最近项目里需要判断是否为手机号并发送验证码的功能,一下是实现方法.不过这个方法还是有些不足,只能判断输入的11位数的号段是否正确,无法判断手机号是否存在.不过勉强可以使用! + (NSString * ...

  5. iOS中判断基础字符(大小写、数字等的判断)

    函数:isdigit 用法:#include 功能:判断字符c是否为数字 说明:当c为数字0-9时,返回非零值,否则返回零. 函数:islower 用法:#include 功能:判断字符c是否为小写英 ...

  6. IOS 开发qq登陆界面

    // //  ViewController.m //  QQUI_bydfg // //  Created by Kevin_dfg on 16/4/15. //  Copyright © 2016年 ...

  7. iOS开发 QQ粘性动画效果

    QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00  博客园精华区 原文  http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...

  8. 微信、QQ和手机号之间不得不说的故事!

    发文字,发图片,发心情,视频聊天,查看附近的人,微信能干的事情QQ都可以,那么它们有什么区别,我QQ用得好好的为什么要我联系人都导到微信去?我们很早就有了QQ,但是在QQ时代,我们虽然用QQ发消息聊天 ...

  9. iOS调用QQ发起临时会话

    iOS调用QQ发起临时会话 iOS调用qq前先判断是否安装qq, 之后通过OpenURL打开对用的qq NSURL *url = [NSURL URLWithString:@"mqq://& ...

  10. 判断qq浏览器和uc浏览器?

    判断在iphone手机上打开的是uc浏览器还是qq浏览器 <html lang="en"> <head> <meta charset="ut ...

随机推荐

  1. python初探-copy

    python中,数据的拷贝有以下三种形式:赋值.浅copy和深copy.根据类型的不同,可以把数据分成以下两类:字符串和数字为一类,其他(包括列表.元祖.字典...)为一类. 在python中有池的概 ...

  2. C++ 用libcurl库进行http 网络通讯编程

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

  3. ajenti试用感受

    ajenti试用感受_展现技术动态_百度空间 ajenti试用感受   今天看开源中国介绍一款名为"服务器管理系统"的开源软件发布了,名为ajenti,页面感觉不错,对机器的采集信 ...

  4. oracle 中的select ...connect by prior ...start with 及(+)的用法

    1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...

  5. js动画学习(一)

    一.运动框架实现思路 1.匀速运动(属性值匀速变化)(改变 left, right, width, height, opacity 等): 2.缓冲运动(属性值的变化速度与当前值与目标值的差成正比): ...

  6. js正则表达式验证字符长度

    原理,就是把一个汉字替换为两个字母,来实现长度验证. //js正则验证字符长度 第一种:直接输出长度 alert('1sS#符'.replace(/[^\x00-\xff]/g, 'AA').leng ...

  7. Thread interrupt方法解析

    初步理解 我们在看一些多线程代码的时候,有的时候会碰到使用interrupt()方法的时候.从字面的意思来理解,应该就是中断当前正在执行的线程.那么,对于一个我们设计的普通线程来说,如果我们在主线程里 ...

  8. JavaSE学习总结第04天_Java基础语法3

      04.01 选择结构switch语句的格式及其解释 switch语句的格式: switch(表达式) {    case 值1:语句体1;break;    case 值2:语句体2;break; ...

  9. _int64、long long 的区别

    C++的64位整数[原]by 赤兔 http://baike.baidu.com/view/1235293.htm 在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其 ...

  10. BZOJ 1295: [SCOI2009]最长距离( 最短路 )

    把障碍点看做点(边)权为1, 其他为0. 对于每个点跑spfa, 然后和它距离在T以内的就可以更新答案 ------------------------------------------------ ...