How to determine if NSString contains a single capital letter or not

I have a textField where I enter the password. Can someone tell me how I can check the text that I entered contains one capital letter or not? I went through the Internet, but could not get what I want. Thanks in advance.

Here I want to add checks.

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];
}
+4
source share
6 answers

just use the code below to confirm your password.

Add method

- (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;
}

Use this code to check the status.

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];
}
+4
source

Here, use your string from UITextFieldor from any other source to find it in any uppercaseor not.

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 regex

- (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");
}
+12

Use this to determine if there is another uppercase letter in the line:

 NSString *string = @"Test";
NSString *lowercaseString = [string lowercaseString];

BOOL containsUppercaseLetter = ![string isEqualToString:lowercaseString];
+5
source

Try it. It will check for two passwords. 1. The password must contain one capital and one small alphabet. 2. No special character is allowed.

- (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
source
-(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;

    }
}
0
source

try it

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")
-1
source

All Articles