1

I Have a text field that I want to enter password in that.I want to enter strong password.That means 8 to 15 characters in that at least one small Letter,one Capital Letter,1 spacial caracter,one number. Please give the suggestion.

I'm a Learner
  • 193
  • 1
  • 2
  • 15
  • check out this answer.. http://stackoverflow.com/questions/15132276/password-validation-in-uitextfield-in-ios – Paras Joshi Apr 26 '13 at 11:17
  • There are a number of interesting articles on the use of pass phrases over complex short passwords for providing memorable secure access, can I recommend checking them out before going down the complex password route? In my experience users are rarely good at managing complex passwords. – Al. Apr 26 '13 at 11:19
  • 1
    Regex is the way to go for this. Check out this http://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper – Mani Apr 26 '13 at 11:31

3 Answers3

5

With

password.length

you can ask for the length of a string. Compare that to your desired limits.

With

- (BOOL)string:(NSString *)text matches:(NSString *)pattern
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

    NSArray *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];

    return matches.count > 0;
}

you have a method that provides regex to strings (you can also implement this as a category to NSString).

The first parameter will be your password, the second will be the pattern.

I am not that good with regex, so there might be better solutions but this would be my way

NSString *password = @"iS_bhd97zAA!";
NSString *scPattern = @"[a-z]";
NSString *cPattern = @"[A-Z]";
NSString *sPattern = @"[!%&\._;,]";
NSString *nPattern = @"[0-9]";

if (8 <= password.length && password.length <= 15 &&
    [self string:password matches:scPattern] &&
    [self string:password matches:cPattern] &&
    [self string:password matches:sPattern] &&
    [self string:password matches:nPattern]) 
{
    NSLog(@"PW is valid");
}

Hint

The regex for special characters is tricky because you need to escape some of the chars. Mine might be correct, but I am not absolutely sure.

There is also a possiblility to do this in only one regex, but this looks scary imo

This one

 (?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

has everything except the special chars, maybe you want to add that yourself :D

Marc
  • 6,051
  • 5
  • 26
  • 56
2

This can be easily done using Regular Expressions. I'm not familiar with Regular Expressions, so I'm suggesting this hard way.

You can use this function for checking this:

- (BOOL)strongPassword:(NSString *)yourText
{
  BOOL strongPwd = YES;

  //Checking length
  if([yourText length] < 8)
       strongPwd = NO;

  //Checking uppercase characters
  NSCharacterSet *charSet = [NSCharacterSet uppercaseLetterCharacterSet];
  NSRange range = [yourText rangeOfCharacterFromSet:charSet];
  if(range.location == NSNotFound)
      strongPwd = NO;

  //Checking lowercase characters
  charSet = [NSCharacterSet lowercaseLetterCharacterSet];
  range = [yourText rangeOfCharacterFromSet:charSet];
  if(range.location == NSNotFound)
      strongPwd = NO;

  //Checking special characters
  charSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
  range = [yourText rangeOfCharacterFromSet:charSet];
  if(range.location == NSNotFound)
      strongPwd = NO;

  return strongPwd;
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • this can be done easy way using regex [Check this](http://stackoverflow.com/questions/5318088/regular-expression-for-password-in-iphone). – Janak Nirmal Apr 26 '13 at 11:25
  • @JanakNirmal: I edited my answer. I don't know how to write Regular Expression in iOS. So I suggested this hard way :) – Midhun MP Apr 26 '13 at 11:34
-2

In the delegate method textField:shouldChangeCharactersInRange:replacementString: for the UITextField you need to do the following:

  • Get text with the property myTextField.text
  • Check for uppercase and lower case character counts
  • Similarly check for the special character and the number counts

If all conditions are satisfied process the field else display error.

I would have written down the code for you but its against the SO policies. You attempt writing this code and if you are stuck you can always post another question.

Praveen S
  • 10,355
  • 2
  • 43
  • 69