我的 iPhone 应用程序中有 1 个 UITextfield 用于密码.
I have 1 UITextfield for password in my iPhone application.
我想通过以下验证来验证此文本字段.
I want to validate this textfield with the following validation.
@#$%^&+=^.*(?=.{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模板网!
如何设置 Edittext 视图只允许两个数值和两个十进How to set Edittext view allow only two numeric values and two decimal values like ##.##(如何设置 Edittext 视图只允许两个数值和两个十进
Android:如何在 EditText 中设置可接受的数字和字符Android : How to set acceptable numbers and characters in EditText?(Android:如何在 EditText 中设置可接受的数字和字符?)
KIF:如何自动运行/压力测试 iOS 应用程序以找出罕KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自动运行/压力测试 iOS 应用程序以找出罕见 UI 错
验证编辑文本Validation on Edit Text(验证编辑文本)
无法更改 Xcode 4.5 中的目标成员身份可见性Can#39;t change target membership visibility in Xcode 4.5(无法更改 Xcode 4.5 中的目标成员身份可见性)
UITableView:在混合单元格表视图静态和动态单元格UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合单元格表视图静态和动态单元格中