IOS校验验证手机号,xyk号,电话号码,邮编,Email等

IOS校验验证手机号,xyk号,电话号码,邮编,Email等,第1张

概述IOS校验验证手机号,xyk号,电话号码,邮编,Email等

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#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._%+-][email protected][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,{3,4},{4,-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,1,6,3,7,2 };//加权因子 const int checktable[] = { 1,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;}/* * 常用xyk卡号规则 * 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  * * xyk号验证基本算法:  * 偶数位卡号奇数位上数字*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],188     12         *///    Nsstring * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\d)\d{7}$";//    /**//     15         * 中国联通:China Unicom//     16         * 130,186//     17         */    Nsstring * CU = @"^1(3[0-2]|5[256]|8[56])\d{8}$";//    /**//     20         * 中国电信:China Telecom//     21         * 133,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) 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

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的IOS校验验证手机号,xyk号,电话号码,邮编,Email等全部内容,希望文章能够帮你解决IOS校验验证手机号,xyk号,电话号码,邮编,Email等所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存