Tirthendu Tirthendu - 4 months ago 22
iOS Question

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

Answer

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