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];
}
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");
}
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With