本文介紹了iOS UITextField 中的密碼驗證的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的 iPhone 應用程序中有 1 個 UITextfield
用于密碼.
I have 1 UITextfield
for password in my iPhone application.
我想通過以下驗證來驗證此文本字段.
I want to validate this textfield with the following validation.
- 必須至少為 10 個字符
- 必須至少包含一個小寫字母、一個大寫字母、一個數(shù)字和一個特殊字符
- 有效的特殊字符為 –
@#$%^&+=^.*(?=.{10,})(?=.*d)(?=.*[az])(?=.*[AZ])(?=.*[@#$%^&+=]).*$
如何限制 UITextField
滿足上述要求?
How can I restrict the UITextField
with above requirements?
推薦答案
我會這樣做.驗證應該在用戶輸入密碼時完成,而不是在兩者之間.我不會使用 NSRegularExpression
.
This is how I would do it. The validation should be done at the end when the user has typed in the password and not in between.I will not be using NSRegularExpression
.
-(void)textFieldDidEndEditing:(UITextField *)textField{
int numberofCharacters = 0;
BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0;
if([textField.text length] >= 10)
{
for (int i = 0; i < [textfield.text length]; i++)
{
unichar c = [textfield.text characterAtIndex:i];
if(!lowerCaseLetter)
{
lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
}
if(!upperCaseLetter)
{
upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
}
if(!digit)
{
digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
}
if(!specialCharacter)
{
specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
}
}
if(specialCharacter && digit && lowerCaseLetter && upperCaseLetter)
{
//do what u want
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please Enter at least 10 password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
希望這會有所幫助...
Hope this helps...
這篇關于iOS UITextField 中的密碼驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!