您好,登錄后才能下訂單哦!
小編給大家分享一下正則表達(dá)式如何實(shí)現(xiàn)驗(yàn)證用戶名、密碼、手機(jī)號(hào)碼、身份證功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
示例
//用戶名 + (BOOL) validateUserName:(NSString *)name { NSString *userNameRegex = @"^[A-Za-z0-9]{3,20}+$"; NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; BOOL B = [userNamePredicate evaluateWithObject:name]; return B; } //密碼 + (BOOL) validatePassword:(NSString *)passWord { NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$"; NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex]; return [passWordPredicate evaluateWithObject:passWord]; } //判斷手機(jī)號(hào)碼格式是否正確 + (BOOL)valiMobile:(NSString *)mobile { mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""]; if (mobile.length != 11) { return NO; }else{ /** * 移動(dòng)號(hào)段正則表達(dá)式 */ NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$"; /** * 聯(lián)通號(hào)段正則表達(dá)式 */ NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$"; /** * 電信號(hào)段正則表達(dá)式 */ NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$"; NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM]; BOOL isMatch2 = [pred1 evaluateWithObject:mobile]; NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM]; BOOL isMatch3 = [pred2 evaluateWithObject:mobile]; NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM]; BOOL isMatch4 = [pred3 evaluateWithObject:mobile]; if (isMatch2 || isMatch3 || isMatch4) { return YES; }else{ return NO; } } } /** * 驗(yàn)證身份證號(hào)碼是否正確的方法 * * @param IDNumber 傳進(jìn)身份證號(hào)碼字符串 * * @return 返回YES或NO表示該身份證號(hào)碼是否符合國家標(biāo)準(zhǔn) */ + (BOOL)isCorrect:(NSString *)IDNumber { NSMutableArray *IDArray = [NSMutableArray array]; // 遍歷身份證字符串,存入數(shù)組中 if (IDNumber.length == 18) { for (int i = 0; i < 18; i++) { NSRange range = NSMakeRange(i, 1); NSString *subString = [IDNumber substringWithRange:range]; [IDArray addObject:subString]; } }else{ for (int i = 0; i < 15; i++) { NSRange range = NSMakeRange(i, 1); NSString *subString = [IDNumber substringWithRange:range]; [IDArray addObject:subString]; } } // 系數(shù)數(shù)組 NSArray *coefficientArray = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil]; // 余數(shù)數(shù)組 NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil]; // 每一位身份證號(hào)碼和對應(yīng)系數(shù)相乘之后相加所得的和 int sum = 0; if (IDNumber.length == 18) { for (int i = 0; i < 17; i++) { int coefficient = [coefficientArray[i] intValue]; int ID = [IDArray[i] intValue]; sum += coefficient * ID; } }else{ for (int i = 0; i < 14; i++) { int coefficient = [coefficientArray[i] intValue]; int ID = [IDArray[i] intValue]; sum += coefficient * ID; } } // 這個(gè)和除以11的余數(shù)對應(yīng)的數(shù) NSString *str = remainderArray[(sum % 11)]; // 身份證號(hào)碼最后一位 NSString *string; if (IDNumber.length == 18) { string = [IDNumber substringFromIndex:17]; }else{ string = [IDNumber substringFromIndex:14]; } // 如果這個(gè)數(shù)字和身份證最后一位相同,則符合國家標(biāo)準(zhǔn),返回YES if ([str isEqualToString:string]) { return YES; } else { return NO; } }
以上是“正則表達(dá)式如何實(shí)現(xiàn)驗(yàn)證用戶名、密碼、手機(jī)號(hào)碼、身份證功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。