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ú)效)