限制輸入密碼長(zhǎng)度

1、代碼實(shí)現(xiàn)"密碼至少為9位,并需包含大寫(xiě)字母、小寫(xiě)字母、數(shù)字或特殊字符等三種"

返回0、1、2為格式不正確,返回4為密碼格式正確

-(int)checkIsHaveNumAndLetter:(NSString*)password

{

//數(shù)字條件

NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];

//符合數(shù)字條件的有幾個(gè)字節(jié)

NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password

options:NSMatchingReportProgress

range:NSMakeRange(0, password.length)];

//英文字條件

NSRegularExpression *sLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[a-z]" options:NSRegularExpressionDotMatchesLineSeparators error:nil];

//符合英文字條件的有幾個(gè)字節(jié)

NSUInteger sLetterMatchCount = [sLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

//英文字條件

NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Z]" options:NSRegularExpressionDotMatchesLineSeparators error:nil];

//符合英文字條件的有幾個(gè)字節(jié)

NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

if (password.length < 9) {

// 密碼長(zhǎng)度不正確

return 0;

} else {

// 沒(méi)有大寫(xiě)或小寫(xiě)

if (tLetterMatchCount == 0 || sLetterMatchCount == 0) {

return 1;

} else {

if (tNumMatchCount > 0) {

return 4;

} else{

if(tNumMatchCount + tLetterMatchCount + sLetterMatchCount < password.length){

return 4;

} else{

return 2;

}

}

}

}

}

需注意:NSRegularExpressionOptions,如果不區(qū)分大小寫(xiě)可以使用NSRegularExpressionCaseInsensitive

NSRegularExpressionCaseInsensitive? ? ? ? ? ? ? = 1 << 0,? // 不區(qū)分大小寫(xiě)的NSRegularExpressionAllowCommentsAndWhitespace? = 1 << 1,? // 忽略空格和# -NSRegularExpressionIgnoreMetacharacters? ? ? = 1 << 2,? // 整體化NSRegularExpressionDotMatchesLineSeparators? = 1 << 3,? // 匹配任何字符,包括行分隔符NSRegularExpressionAnchorsMatchLines? ? ? ? ? ? ? ? ? = 1 << 4,? // 允許^和$在匹配的開(kāi)始和結(jié)束行NSRegularExpressionUseUnixLineSeparators? ? ? = 1 << 5,? // (查找范圍為整個(gè)的話無(wú)效)NSRegularExpressionUseUnicodeWordBoundaries? = 1 << 6? ? // (查找范圍為整個(gè)的話無(wú)效)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容