五.彈出警告框+正則身份證與手機(jī)號(hào)

(1)點(diǎn)確定無操作

 UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"時(shí)間8:00 - 18:00" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *actions = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                  {
                                      
                                  }];
        [alert1 addAction:actions];
        
        [self presentViewController:alert1 animated:YES completion:nil];

(2)點(diǎn)確定有操作

                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請(qǐng)開啟視頻" message:nil preferredStyle:UIAlertControllerStyleAlert];
        //如果是取消  就選取消style
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        //如果確定  且有動(dòng)作  就在handler 里進(jìn)行block操作
                UIAlertAction *determine = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            ViewController *vc = [[ViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }];
        //添加取消和確定動(dòng)作
                [alert addAction:cancel];
                [alert addAction:determine];
        //模態(tài)彈出
                [self presentViewController:alert animated:YES completion:nil];

(3)有輸入框警告

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"請(qǐng)輸入姓名" preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"輸入姓名";
            textField.secureTextEntry = NO;
        }];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            UITextField *text  = alert.textFields.firstObject;
            
            if (text.text.length==0)
            {
                MBProgressHUD *hud = [Utils createHUD];
                [hud hide:YES afterDelay:5];
                _namelabel.text=@"必填";
                _namelabel.textColor = [UIColor lightGrayColor];

                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"輸入的名字不能為空";
            }else{
            
            _name=_namelabel.text=text.text;
            _namelabel.textColor = [UIColor themeBlackColor];
            }
        }];
        UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:no];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];

(4)彈出有textField的 且里面有HUD的

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"請(qǐng)輸入患者身份證號(hào)碼" preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"輸入身份證號(hào)碼";
            textField.keyboardType=UIKeyboardTypeEmailAddress;
//輸入需要帶******號(hào)嗎
            textField.secureTextEntry = NO;
//            
           
        }];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           
            UITextField *text  = alert.textFields.firstObject;
             // 號(hào)碼為空  后者少于18位
                    if (text.text.length==0||text.text.length<18)
                    {
                        _idnum=_idnumlabel.text=@"選填";
                        _idnumlabel.textColor = [UIColor lightGrayColor];
                        // 顯示 MBProgressHUD(需要在主線程中顯示)
                        dispatch_async(dispatch_get_main_queue(), ^{
                            MBProgressHUD *hud = [Utils createHUD1];
                            hud.mode = MBProgressHUDModeText;
                            hud.labelText = @"您輸入號(hào)碼有誤";
                            [hud hide:YES afterDelay:1.5];
                        });
                        return ;
                    }
                    //如果18位  但是不正確或準(zhǔn)確
                     if (text.text.length==18)
                     {
                         //如果號(hào)碼  是18位且滿足驗(yàn)證
                         if ([self checkUserIdCard:text.text])
                         {
                             
                             _idnum=_idnumlabel.text=text.text;
                             _idnumlabel.textColor = [UIColor themeBlackColor];
                             // 顯示 MBProgressHUD(需要在主線程中顯示)
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 MBProgressHUD *hud = [Utils createHUD1];
                                 hud.mode = MBProgressHUDModeText;
                                 hud.labelText = @"一定要核實(shí)準(zhǔn)確哦~~";
                                 [hud hide:YES afterDelay:1.5];
                             });
                             return ;
                         }else
                         {
                             _idnum=_idnumlabel.text=@"選填";
                             _idnumlabel.textColor = [UIColor lightGrayColor];
                             // 顯示 MBProgressHUD(需要在主線程中顯示)
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 MBProgressHUD *hud = [Utils createHUD1];
                                 hud.mode = MBProgressHUDModeText;
                                 hud.labelText =  @"該身份證賬號(hào)不存在";
                                 [hud hide:YES afterDelay:1.5];
                             });
                             return ;                      
                         }
                     }
            
            
            
        }];
        UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:no];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];



判斷手機(jī)號(hào)的
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"請(qǐng)輸入患者手機(jī)號(hào)碼" preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"輸入手機(jī)號(hào)碼";
            textField.keyboardType=UIKeyboardTypeNumberPad;

            textField.secureTextEntry = NO;
        }];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            UITextField *text  = alert.textFields.firstObject;
            // 號(hào)碼為空  后者少于11位
            if (text.text.length==0||text.text.length<11)
            {
                _mobile= _mobilelabel.text=@"選填";
                _mobilelabel.textColor = [UIColor lightGrayColor];
                // 顯示 MBProgressHUD(需要在主線程中顯示)
                dispatch_async(dispatch_get_main_queue(), ^{
                    MBProgressHUD *hud = [Utils createHUD1];
                    hud.mode = MBProgressHUDModeText;
                    hud.labelText = @"您輸入號(hào)碼有誤";
                    [hud hide:YES afterDelay:1.5];
                });
                return ;
            }
            //如果是11位
            if (text.text.length==11)
            {
                //如果滿足
                if ([self validateMobile:text.text])
                {
                    _mobile= _mobilelabel.text=text.text;
                    _mobilelabel.textColor = [UIColor themeBlackColor];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        MBProgressHUD *hud = [Utils createHUD1];
                        hud.mode = MBProgressHUDModeText;
                        hud.labelText = @"一定要核實(shí)準(zhǔn)確哦~~";
                        [hud hide:YES afterDelay:1.5];
                    });
                    return ;
                    
                }else{
                    _mobile= _mobilelabel.text=@"選填";
                    _mobilelabel.textColor = [UIColor lightGrayColor];
                    // 顯示 MBProgressHUD(需要在主線程中顯示)
                    dispatch_async(dispatch_get_main_queue(), ^{
                        MBProgressHUD *hud = [Utils createHUD1];
                        hud.mode = MBProgressHUDModeText;
                        hud.labelText = @"該手機(jī)賬號(hào)不存在";
                        [hud hide:YES afterDelay:1.5];
                    });
                    return ;
                
                
                }
  
                
            }
            
                               
        }];
        UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:no];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];

//判斷手機(jī)號(hào)正則



- (BOOL) validateMobile:(NSString *)mobile
{
    //手機(jī)號(hào)以13劫映, 15,18開頭,八個(gè) \d 數(shù)字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

//判斷身份證號(hào)正則

#pragma 正則匹配用戶身份證號(hào)18位

- (BOOL)checkUserIdCard: (NSString *) identityCard
{
    BOOL flag;
    if (identityCard.length <= 0)
    {
        flag = NO;
        return flag;
    }
    
    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    flag = [identityCardPredicate evaluateWithObject:identityCard];
    
    
    //如果通過該驗(yàn)證靶橱,說明身份證格式正確萨西,但準(zhǔn)確性還需計(jì)算
    if(flag)
    {
        if(identityCard.length==18)
        {
            //將前17位加權(quán)因子保存在數(shù)組里
            NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
            
            //這是除以11后,可能產(chǎn)生的11位余數(shù)、驗(yàn)證碼题山,也保存成數(shù)組
            NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
            
            //用來保存前17位各自乖以加權(quán)因子后的總和
            
            NSInteger idCardWiSum = 0;
            for(int i = 0;i < 17;i++)
            {
                NSInteger subStrIndex = [[identityCard substringWithRange:NSMakeRange(i, 1)] integerValue];
                NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
                
                idCardWiSum+= subStrIndex * idCardWiIndex;
                
            }
            
            //計(jì)算出校驗(yàn)碼所在數(shù)組的位置
            NSInteger idCardMod=idCardWiSum%11;
            
            //得到最后一位身份證號(hào)碼
            NSString * idCardLast= [identityCard substringWithRange:NSMakeRange(17, 1)];
            
            //如果等于2坯临,則說明校驗(yàn)碼是10焊唬,身份證號(hào)碼最后一位應(yīng)該是X
            if(idCardMod==2)
            {
                if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
                {
                    return flag;
                }else
                {
                    flag =  NO;
                    return flag;
                }
            }else
            {
                //用計(jì)算出的驗(yàn)證碼與最后一位身份證號(hào)碼匹配,如果一致看靠,說明通過求晶,否則是無效的身份證號(hào)碼
                if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
                {
                    return flag;
                }
                else
                {
                    flag =  NO;
                    return flag;
                }
            }
        }
        else
        {
            flag =  NO;
            return flag;
        }
    }
    else
    {
        return flag;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市衷笋,隨后出現(xiàn)的幾起案子芳杏,更是在濱河造成了極大的恐慌,老刑警劉巖辟宗,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件爵赵,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡泊脐,警方通過查閱死者的電腦和手機(jī)空幻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來容客,“玉大人秕铛,你說我怎么就攤上這事约郁。” “怎么了但两?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵鬓梅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我谨湘,道長(zhǎng)慨代,這世上最難降的妖魔是什么僻族? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任客年,我火速辦了婚禮烤黍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘擅耽。我一直安慰自己活孩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布乖仇。 她就那樣靜靜地躺著诱鞠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪这敬。 梳的紋絲不亂的頭發(fā)上航夺,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音崔涂,去河邊找鬼阳掐。 笑死,一個(gè)胖子當(dāng)著我的面吹牛冷蚂,可吹牛的內(nèi)容都是我干的缭保。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蝙茶,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼艺骂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起隆夯,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤钳恕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后蹄衷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體忧额,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年愧口,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了睦番。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖托嚣,靈堂內(nèi)的尸體忽然破棺而出巩检,到底是詐尸還是另有隱情,我是刑警寧澤示启,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布兢哭,位于F島的核電站,受9級(jí)特大地震影響丑搔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜提揍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一啤月、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧劳跃,春花似錦谎仲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至杉武,卻和暖如春辙诞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背轻抱。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工飞涂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人祈搜。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓较店,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親容燕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梁呈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容