Ios judges qq, bank card, mobile phone number, and other public methods.
# Import
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}; // Weighting Factor
Const int checktable [] = {1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2}; // check value corresponding to the table
+ (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;
}
/*
* Common credit card number rules
* Issuer Identifier Card Number Length
* Diner's Club 300xxx-305xxx, 3095xx, 36 xxxx, 38 xxxx 14
* American Express 34 xxxx, 37 xxxx 15
* VISA 4 xxxxx 13, 16
* MasterCard 51xxxx-55xxxx 16
* JCB 3528xx-358xxx 16
* Discover 6011xx 16
* UnionPay 622126-622925 16
*
* Basic algorithm for credit card number verification:
* The odd digit number * 2 indicates the odd digit Card Number * 2 indicates the odd digit Card Number * 2 indicates the odd digit card number.
* The number of digits greater than 10 is reduced by 9.
* Add all numbers.
* The result is invalid because it is not a multiple of 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
{
/**
* Mobile phone number
* Move: 134 [0-8], 135,136,137,138,139,150,151,157,158,159,182,187,188
* Unicom: 130,131,132,152,155,156,185,186
* China Telecom: 153,180,189
*/
// NSString * MOBILE = @ "^ 1 (3 [0-9] | 5 [0-35-9] | 8 [025-9]) \ d {8} $ ";
/**
10 * China Mobile: 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: 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: China Telecom
// 21*133,1349, 153,180,189
// 22 */
// NSString * CT = @ "^ 1 (33 | 53 | 8 [09]) [0-9] | 349) \ d {7} $ ";
///**
// 25 * fixed telephones and PHS in Mainland China
// 26 * area code: 010,020,021,022,023,024,025,027,028,029
// 27 * Number: seven or eight digits
// 28 */
// NSString * PHS = @ "^ 0 (10 | 2 [0-5789] | \ d {3}) \ d {} $ ";
// NSString * PHS1 = @ "^ 0 (10 | 2 [0-5789] | \ d {3}-) \ d {} $ ";
// 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