iOS 密碼等級驗證

iOS密碼等級驗證效果如圖 ,原理是判斷是否有數字,字母特殊字符來區別等級顯示                                

                                                                  

-(NSString *)blackClassLblText:(NSString*)str

{

        NSMutableArray* resultArray = [[NSMutableArrayalloc] init];

        NSArray* termArray1 = [[NSArrayalloc] initWithObjects:@"a",@"b", @"c",@"d", @"e",@"f", @"g",@"h", @"i",@"j", @"k",@"l", @"m",@"n", @"o",@"p", @"q",@"r", @"s",@"t", @"u",@"v", @"w",@"x", @"y",@"z", nil];

        

        NSArray* termArray2 = [[NSArrayalloc] initWithObjects:@"1",@"2", @"3",@"4", @"5",@"6", @"7",@"8", @"9",@"0", nil];

        

        NSArray* termArray3 = [[NSArrayalloc] initWithObjects:@"A",@"B", @"C",@"D", @"E",@"F", @"G",@"H", @"I",@"J", @"K",@"L", @"M",@"N", @"O",@"P", @"Q",@"R", @"S",@"T", @"U",@"V", @"W",@"X", @"Y",@"Z", nil];

        

        NSArray* termArray4 = [[NSArrayalloc] initWithObjects:@"~",@"`",@"@",@"#",@"$",@"%",@"^",@"&",@"*",@"(",@")",@"-",@"_",@"+",@"=",@"{",@"}",@"[",@"]",@"|",@":",@";",@"“",@"'",@"‘",@"<",@",",@".",@">",@"?",@"/",@"",nil];

        

        NSString* result1 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray1 Password:str]];

        

        NSString* result2 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray2 Password:str]];

        

        NSString* result3 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray3 Password:str]];

        

        NSString* result4 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray4 Password:str]];

    

        [resultArray addObject:[NSStringstringWithFormat:@"%@",result1]];

        

        [resultArray addObject:[NSStringstringWithFormat:@"%@",result2]];

        

        [resultArray addObject:[NSStringstringWithFormat:@"%@",result3]];

        

        [resultArray addObject:[NSStringstringWithFormat:@"%@",result4]];

    

        int intResult=0;

        

        for (int j=0; j<[resultArraycount]; j++)

            

        { 

            if ([[resultArrayobjectAtIndex:j] isEqualToString:@"1"])

                

            {    

                intResult++; 

            }

            

        }

        

        NSString* resultString = [[NSStringalloc] init];

        

        if (intResult <2)

            

        {

            resultString = @"低級";

        }

        

        elseif (intResult == 2)

        

        {

            resultString = @"中級";

        }

        

        if (intResult >2)

            

        {

            

            resultString = @"高級";

            

        }


        return resultString;

    

}

-(BOOL)judgeRange:(NSArray*) _termArray Password:(NSString*) _password

{

    NSRange range;

    

    BOOL result =NO;

    

    for(int i=0; i<[_termArraycount]; i++)

        

    {

        range = [_password rangeOfString:[_termArrayobjectAtIndex:i]];

        

        if(range.location !=NSNotFound)  

        {

            result =YES;

        }

    }

    return result;  

}

//代理裏邊控制

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    if (textField==self.phoneTextfield) {

        self.classLbl2.hidden=NO;

        self.classLbl2.text=[selfblackClassLblText:self.phoneTextfield.text];

        if ([self.classLbl2.textisEqualToString:@"低級"]) {

   //設置字體顏色RGBA(24360621) 爲宏定義

            self.classLbl2.textColor=RGBA(243,60, 62,1);

        }else if ([self.classLbl2.textisEqualToString:@"中級"])

        {

            self.classLbl2.textColor=RGBA(255,185, 42,1);

        }else

        {

            self.classLbl2.textColor=RGBA(41,184, 27,1);

        }

        

    }

    return YES;

}




發佈了73 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章