我想从具有以下要求的文本字段验证我的密码。
我尝试了[[NSCharacterSet symbolCharacterSet] characterIsMember:(my character to be tested]
,但它并不适用于所有角色。
如果可能的话,建议我使用正则表达式的解决方案,谢谢。
答案 0 :(得分:4)
试一试。
- (BOOL)isPasswordValid:(NSString *)password
{
NSCharacterSet * characterSet = [NSCharacterSet uppercaseLetterCharacterSet] ;
NSRange range = [password rangeOfCharacterFromSet:characterSet] ;
if (range.location == NSNotFound) {
return NO ;
}
characterSet = [NSCharacterSet lowercaseLetterCharacterSet] ;
range = [password rangeOfCharacterFromSet:characterSet] ;
if (range.location == NSNotFound) {
return NO ;
}
characterSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] ;
for (NSUInteger i = 0; i < [password length]; ++i) {
unichar uchar = [password characterAtIndex:i] ;
if (![characterSet characterIsMember:uchar]) {
return NO ;
}
}
return YES ;
}
答案 1 :(得分:0)
对于大写和小写,您可以使用以下代码:
[[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[myString characterAtIndex:0]];
对于特殊字符,您可以使用以下代码:
if ([aString isMatchedByRegex:@"[^a-zA-Z0-9]"]) {
NSLog(@"This string contains illegal characters");
}