Tirthendu
Tirthendu

Reputation: 65

How to detect whether a NSString contains one capital letter or not

I have a textField where I am entering password. Can anyone please tell me how can I check the text that I am entering contains one capital letter or not? I have gone through web but failed to get what exactly I am wanting. Thanks in advance.

This is where I want to add the checkins.

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

Upvotes: 4

Views: 4300

Answers (6)

Pankaj Gupta
Pankaj Gupta

Reputation: 146

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

    }
}

Upvotes: 0

Nirmal Choudhari
Nirmal Choudhari

Reputation: 559

simply use below code to validate 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 for checking condition.

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

Upvotes: 6

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

Here use your string from UITextField or any other source to find it contains any uppercase or 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");
}

EDIT According to your requirement : 1. Minimum 6 characters. 2. At least one of them should be caps. so Nirmal Choudhari's regex can used with following method for checking its valid or not

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

Usage :

NSString *str = @"appLe";
BOOL isValid = [self containsValidPassword:str];
if (isValid) {
    NSLog(@"valid");
} else {
    NSLog(@"not valid");
}

Upvotes: 13

Christian
Christian

Reputation: 382

Try this

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

Upvotes: -1

Naeem
Naeem

Reputation: 789

Try This out. It Will Check for two password validation. 1. 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 ;
}

Upvotes: 2

David Silverfarmer
David Silverfarmer

Reputation: 455

Use this to determine wether there is one ore more uppercase letters in the string:

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

BOOL containsUppercaseLetter = ![string isEqualToString:lowercaseString];

Upvotes: 5

Related Questions