0

I am trying this for the first time.I need to set some validations for my sign up page .It has 3 fields:name,email and password.I have successfully set the validations for the first two fields but things are getting a bit messy with the third field.Here is my code for the third field:

    NSString *string = @"1Aa1A111";
    NSString *format = [NSString stringWithFormat:@"([A-Z])*(([a-z])+ ([0-9])+([A-Z])+)([A-Z])*"];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",format];
    BOOL match = [pred evaluateWithObject:string];

Now when I tried to google the answer I'm getting a whole lot of answers that I can't understand , specially "?=" in regular expression.I tried to check the functionality of "?=" but my code crashes everytime.Here is my code for checking the functionality of "?=" :

     NSString *string = @"a";
     NSString *format = [NSString stringWithFormat:@"?=.*[a-z]"];
     NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",format];
     BOOL match = [pred evaluateWithObject:string];
     NSLog(@"%d",match);

The validations that I need for my password are:

  • Atleast one lowercase.
  • Atleast one uppercase.
  • Atleast a digit.
  • Atleast a special character.
Reckoner
  • 1,041
  • 2
  • 13
  • 29
  • It is usually the same question. `^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\\W)`. – Wiktor Stribiżew Sep 11 '15 at 13:45
  • yes it is but I would like to understand the meaning of "?=.*[a-z]" in that answer of yours rather than the whole answer. – Reckoner Sep 11 '15 at 13:49
  • :) There are tons of such answers with explanations. And rexegg.com where you can read a lot about that. Here is [one of my answers](http://stackoverflow.com/a/30723576/3832970). – Wiktor Stribiżew Sep 11 '15 at 13:50
  • That's exactly what I needed.Thanks a ton ;-) – Reckoner Sep 11 '15 at 13:57
  • Hello I wrote a code for this kind of situations! https://github.com/jasonnam/Navajo-Swift if you have any questions feel free to email me :) – Jason Nam Sep 11 '15 at 13:59
  • 1
    Your code above mis-uses the "stringWithFormat" NSString class method. That method serves no useful purpose, may cause problems, and should not be used. replace that line with `NSString *format = @"?=.*[a-z]";`. (stringWithFormat creates an NSString using C style string formatting symbols. It has nothing to do with getting a string ready for use in an NSPredicate, and should not be used like that.) – Duncan C Sep 11 '15 at 14:08

0 Answers0