我正在开发一个与货币交易相关的应用程序。因此,在用户注册屏幕中,我想对密码字段强加限制。
这里,要求是,
1)密码必须是一个小写字母,一个大写字母,一个特殊字符 (包括所有类型的特殊字符。) 。
2)空格和数字不是强制性的,但如果用户想要输入则接受它,
3)密码长度必须大于6 char。
低于我的所作所为。
-(BOOL) isPasswordValid:(NSString *)pwd
{
NSString *password = pwd;
NSLog(@"Password ==> %@",pwd);
NSString *scPattern = @"[a-z]";
NSString *cPattern = @"[A-Z]";
// !~`@#$%^&*-+();:={}[],\'.<>?\\/\"
// @"[@!%&\._;,]"
NSString *sPattern = @"^\\w+( +\\w+)*$";
if ((6 <= password.length) && [self string:password matches:scPattern] && [self string:password matches:cPattern] && [self string:password matches:sPattern])
{
return YES;
}
else
return NO;
}
我在上面调用函数
if([self isPasswordValid])
{
/// Your password is valid;
}
else
{
/// Your password is not valid
}
当我输入 @ Aa123#"
时,但每次我 /// Your password is not valid
任何人都知道如何获得预期结果?通过正则表达式或任何其他技巧?
感谢。
答案 0 :(得分:2)
请试试这个
-(void)textFieldDidEndEditing:(UITextField *)textField{
int numberofCharacters = 0;
BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0;
if([textField.text length] >= 6)
{
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 6 digit password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}