我有一个textField,我输入密码。 任何人都可以告诉我如何检查我输入的文本是否包含一个大写字母? 我已经通过网络但未能得到我想要的东西。提前谢谢。
这是我想添加签到的地方。
if([self.txtfldPw.text isEqualToString:@""] && [self.txtfldPw.text.length = ] && [self.txtfldEmail.text = ]) {
UIAlertView *pwAlrt = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Password Must Be Of Six Characters And One Of The Letters Should Be Caps" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[pwAlrt show];
[self.btnLogin setEnabled:YES];
}
答案 0 :(得分:13)
此处使用UITextField
或任何其他来源的字符串,以查找其中是否包含uppercase
。
NSString *str = @"Apple";
//get all uppercase character set
NSCharacterSet *cset = [NSCharacterSet uppercaseLetterCharacterSet];
//Find range for uppercase letters
NSRange range = [str rangeOfCharacterFromSet:cset];
//check it conatins or not
if (range.location == NSNotFound) {
NSLog(@"not any capital");
} else {
NSLog(@"has one capital");
}
编辑根据您的要求:1。最少6个字符。 2.其中至少有一个应该是上限。所以Nirmal Choudhari
的正则表达式可以使用以下方法来检查其有效与否
- (BOOL)containsValidPassword:(NSString*)strText
{
NSString* const pattern = @"^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z]).*$";
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
NSRange range = NSMakeRange(0, [strText length]);
return [regex numberOfMatchesInString:strText options:0 range:range] > 0;
}
用法:
NSString *str = @"appLe";
BOOL isValid = [self containsValidPassword:str];
if (isValid) {
NSLog(@"valid");
} else {
NSLog(@"not valid");
}
答案 1 :(得分:6)
只需使用以下代码验证您的密码。
添加方法
- (BOOL)isValidPassword:(NSString*)password
{
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z]).*$" options:0 error:nil];
return [regex numberOfMatchesInString:password options:0 range:NSMakeRange(0, [password length])] > 0;
}
使用此代码检查条件。
if(![self isValidPassword:[password stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]) {
UIAlertView *pwAlrt = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Password Must Be Of Six Characters And One Of The Letters Should Be Caps" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[pwAlrt show];
[self.btnLogin setEnabled:YES];
}
答案 2 :(得分:5)
使用此选项可确定字符串中是否有一个或多个大写字母:
NSString *string = @"Test";
NSString *lowercaseString = [string lowercaseString];
BOOL containsUppercaseLetter = ![string isEqualToString:lowercaseString];
答案 3 :(得分:2)
试一试。它将检查两个密码验证。 1.密码必须包含一个大写字母和一个小字母表。 2.不允许特殊字符。
- (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 ;
}
答案 4 :(得分:0)
-(BOOL)passwordvarify:(NSString*)password // pass your password here it will return true if it contains one uppercase letter else false just call this method and evaluate your password on the basis of true and false
{
int count=0;
for (int i = 0; i < [password length]; i++) {
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[password characterAtIndex:i]];
if (isUppercase == YES)
{
count++;
break;
}
}
if(count == 1)
{
NSLog(@"password have one uppercase letter");
return true;
}
else
{
NSLog(@"password do not have uppercase letter");
return false;
}
}
答案 5 :(得分:-1)
试试这个
NSString *s = @"This is a string";
int count=0;
BOOL isUppercase;
for (i = 0; i < [s length]; i++) {
isUppercase= [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:i]];
if (isUppercase)
break;
}
isUppercase?NSLog("String contain uppercase letter"):NSLog("String does not contain uppercase letter")