溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ios正則表達(dá)式

發(fā)布時(shí)間:2020-07-08 03:10:38 來(lái)源:網(wǎng)絡(luò) 閱讀:331 作者:ljl5125 欄目:移動(dòng)開(kāi)發(fā)

#pragma mark -驗(yàn)證手機(jī)號(hào)碼的正則表達(dá)式

- (BOOL)checkTel:(NSString *)str

{

    if ([str length] == 0) {

        UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:@"手機(jī)號(hào)不能為空" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

        

        [alert  show];

        return NO;

    }

    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[01,5-9]))\\d{8}$";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];    BOOL isMatch = [pred evaluateWithObject:str];

    if (!isMatch) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請(qǐng)輸入正確的手機(jī)號(hào)碼" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

        [alert show];

        return NO;

    }

    return YES;

}




#pragma mark -郵箱驗(yàn)證

-(BOOL)isValidateEmail:(NSString *)email

{

    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:email];

}




#pragma mark - 驗(yàn)證***是否合法

-(BOOL) chk18PaperId:(NSString *) sPaperId

{

    //判斷位數(shù)

    if ([sPaperId length] != 15 && [sPaperId length] != 18) {

        return NO;

    }

    NSString *carid = sPaperId;

    long lSumQT =0;

    

    //加權(quán)因子

    int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

    //校驗(yàn)碼

    unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};

    //15位***號(hào)轉(zhuǎn)換成18

    NSMutableString *mString = [NSMutableString stringWithString:sPaperId];

    

    if ([sPaperId length] == 15) {

        [mString insertString:@"19" atIndex:6];

        long p = 0;

        const char *pid = [mString UTF8String];

        for (int i=0; i<=16; i++)

        {

            p += (pid[i]-48) * R[i];

        }

        int o = p%11;

        NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];

        [mString insertString:string_content atIndex:[mString length]];

        carid = mString;

    }

    

    //判斷地區(qū)碼

    NSString * sProvince = [carid substringToIndex:2];

    if (![self areaCode:sProvince]) {

        return NO;

    }

    

    //判斷年月日是否有效

    //年份

    int strYear = [[self getStringWithRange:carid Value1:6 Value2:4] intValue];

    //月份

    int strMonth = [[self getStringWithRange:carid Value1:10 Value2:2] intValue];

    //

    int strDay = [[self getStringWithRange:carid Value1:12 Value2:2] intValue];

    NSTimeZone *localZone = [NSTimeZone localTimeZone];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    [dateFormatter setTimeZone:localZone];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];

    

    if (date == nil) {

        return NO;

    }

    const char *PaperId  = [carid UTF8String];

    //檢驗(yàn)長(zhǎng)度

    if( 18 != strlen(PaperId)) return -1;

    //校驗(yàn)數(shù)字

    for (int i=0; i<18; i++)

    {

        if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) )

        {

            return NO;

        }

    }

    //驗(yàn)證最末的校驗(yàn)碼

    for (int i=0; i<=16; i++)

    {

        lSumQT += (PaperId[i]-48) * R[i];

    }

    if (sChecker[lSumQT%11] != PaperId[17] )

    {

        return NO;

    }

    return YES;

}


/**

 * 功能:判斷是否在地區(qū)碼內(nèi)

 * 參數(shù):地區(qū)碼

 */


-(BOOL)areaCode:(NSString *)code

{

    

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

    [dic setObject:@"北京" forKey:@"11"];

    [dic setObject:@"天津" forKey:@"12"];

    [dic setObject:@"河北" forKey:@"13"];

    [dic setObject:@"山西" forKey:@"14"];

    [dic setObject:@"內(nèi)蒙古" forKey:@"15"];

    [dic setObject:@"遼寧" forKey:@"21"];

    [dic setObject:@"吉林" forKey:@"22"];

    [dic setObject:@"黑龍江" forKey:@"23"];

    [dic setObject:@"上海" forKey:@"31"];

    [dic setObject:@"江蘇" forKey:@"32"];

    [dic setObject:@"浙江" forKey:@"33"];

    [dic setObject:@"安徽" forKey:@"34"];

    [dic setObject:@"福建" forKey:@"35"];

    [dic setObject:@"江西" forKey:@"36"];

    [dic setObject:@"山東" forKey:@"37"];

    [dic setObject:@"河南" forKey:@"41"];

    [dic setObject:@"湖北" forKey:@"42"];

    [dic setObject:@"湖南" forKey:@"43"];

    [dic setObject:@"廣東" forKey:@"44"];

    [dic setObject:@"廣西" forKey:@"45"];

    [dic setObject:@"海南" forKey:@"46"];

    [dic setObject:@"重慶" forKey:@"50"];

    [dic setObject:@"四川" forKey:@"51"];

    [dic setObject:@"貴州" forKey:@"52"];

    [dic setObject:@"云南" forKey:@"53"];

    [dic setObject:@"西藏" forKey:@"54"];

    [dic setObject:@"陜西" forKey:@"61"];

    [dic setObject:@"甘肅" forKey:@"62"];

    [dic setObject:@"青海" forKey:@"63"];

    [dic setObject:@"寧夏" forKey:@"64"];

    [dic setObject:@"新疆" forKey:@"65"];

    [dic setObject:@"臺(tái)灣" forKey:@"71"];

    [dic setObject:@"香港" forKey:@"81"];

    [dic setObject:@"澳門(mén)" forKey:@"82"];

    [dic setObject:@"國(guó)外" forKey:@"91"];

    

    if ([dic objectForKey:code] == nil) {

        return NO;

    }

    return YES;

}


/**

 * 功能:獲取指定范圍的字符串

 * 參數(shù):字符串的開(kāi)始小標(biāo)

 * 參數(shù):字符串的結(jié)束下標(biāo)

 */

-(NSString *)getStringWithRange:(NSString *)str Value1:(NSInteger)value1 Value2:(NSInteger )value2;

{

    return [str substringWithRange:NSMakeRange(value1,value2)];

}




向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI