Want to improve this question? Update the question so it focuses on one problem only by editing this post.
开发者_JS百科Closed 4 years ago.
Improve this questionI have an application in which I have to perform validations on password and confirm password. This is my criteria for password field:
- First letter should be capital.
- Password should be more than above 6 values.
- At least one special key should be present.
- At least 1 numeric value should be entered in password.
If all this criteria are present then only password is accepted.
This should work:
- (BOOL)passwordIsValid:(NSString *)password {
// 1. Upper case.
if (![[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[password characterAtIndex:0]])
return NO;
// 2. Length.
if ([password length] < 6)
return NO;
// 3. Special characters.
// Change the specialCharacters string to whatever matches your requirements.
NSString *specialCharacters = @"!#€%&/()[]=?$§*'";
if ([[password componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:specialCharacters]] count] < 2)
return NO;
// 4. Numbers.
if ([[password componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]] count] < 2)
return NO;
return YES;
}
Testing it out:
NSLog(@"%@", [self passwordIsValid:@"Test#one"] ? @"YES" : @"NO"); // Prints NO.
NSLog(@"%@", [self passwordIsValid:@"Test#1"] ? @"YES" : @"NO"); // Prints YES.
This is the code: what you need to do is, just manipulate regular expressin according to your need,
-(IBAction)passwordValidator:(id)sender{
NSString *pwd=[NSString stringWithString:passwordField.text];
int lngth=[pwd length];
int minlength=6;
NSString *regex = @"\\b([a-zA-Z0-9]+)\\b";
NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL x= [regextest evaluateWithObject:pwd];
if (lngth>=minlength) {
NSLog(@"passoword length is enough");
if (x==FALSE) {
//DO something
}
}
else {
//DO other thing
}
}
You Can Use Following set of validations.
Use DHValidation for your all validations. It is much easier to use.
精彩评论