0

hi am currently using the following to validate the password but i want to include special characters also. Currently it contains only numbers and alphabets. Please help.

 - (BOOL)validatePassword:(NSString *) password{
    NSString *ACCEPTABLE_CHARECTERS = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS] invertedSet];
    NSString *filtered = [[password componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [password isEqualToString:filtered];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Daniella D'Cruz
  • 388
  • 1
  • 3
  • 15
  • You can look at [this post](http://stackoverflow.com/questions/15132276/password-validation-in-uitextfield-in-ios) or [This one](http://stackoverflow.com/questions/26423427/regular-expression-for-password-validation-objective-c) – manman Oct 27 '15 at 05:07
  • You can look at [this post](http://stackoverflow.com/questions/15132276/password-validation-in-uitextfield-in-ios) or [This one](http://stackoverflow.com/questions/26423427/regular-expression-for-password-validation-objective-c) – manman Oct 27 '15 at 05:08
  • 1
    Did you try adding the special characters to `ACCEPTABLE_CHARECTERS`? BTW - you misspelled "CHARACTERS". – rmaddy Oct 27 '15 at 05:11

3 Answers3

14

You can try this

- (BOOL)validatePassword:(NSString *) password{
    if(password.length == 0){
    return NO;
    }
    NSString *regex = @"^(?=(.*\d){2})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z!@#$%]{8,}";
    NSPredicate *passwordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [passwordPredicate evaluateWithObject:password];
}

EXPLANATION

(?=(.*\d){2}) - uses lookahead (?=) and says the password must contain at least 2 digits

(?=.*[a-zA-Z]) - uses lookahead and says the password must contain an alpha

(?=.*[!@#$%]) - uses lookahead and says the password must contain 1 or more special characters which are defined

[0-9a-zA-Z!@#$%] - dictates the allowed characters

{8,} - says the password must be at least 8 characters long

It might need a little tweaking e.g. specifying exactly which special characters you need but it should do the trick.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Piyush Sharma
  • 1,891
  • 16
  • 23
0
-(BOOL)validatePassword:(NSString *) password{
    NSString *COMMON_CHARECTERS = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    NSString *SPECIAL_CHARECTERS = @"@#$%^&*";
    NSCharacterSet *cs_common = [NSCharacterSet characterSetWithCharactersInString:COMMON_CHARECTERS];
    NSCharacterSet *cs_special = [NSCharacterSet characterSetWithCharactersInString:SPECIAL_CHARECTERS];
    NSString *filtere_common = [[password componentsSeparatedByCharactersInSet:cs_common] componentsJoinedByString:@""];
    NSString *filtere_special = [[password componentsSeparatedByCharactersInSet:cs_special] componentsJoinedByString:@""];
    BOOL valid = (password.length == (filtere_common.length + filtere_special.length));
    return valid;
}
sunyanyan
  • 1
  • 1
-1

You can use regex to identify use of special character. like

^([a-zA-Z+]+[0-9+]+[&@!#+]+)$

You can use character sets as well to validate password:- Check this link

Community
  • 1
  • 1
KavyaKavita
  • 1,581
  • 9
  • 16