ios 判断,qq,银行卡,手机号,邮编,生日,数字,字符串,护照, email

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

上一篇:引入CSS样式表(书写位置)


下一篇:Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏