2

My password regex is

- (NSString *) getRegularExpression:(NSString *)extraWords
{
    /*
     *  ^$          Empty string
     *  |           Or
     *  ^           Start of a string
     *  []          Explicit set of characters to match
     *  ^[:space:]  Matches any non-white-space character - equivalent to \S
     *  {6,30}      6-20 characters
     *  $           End of a string
     */
    return @"^$|^[^[:space:]]{6,20}$";
}

How can I write a confirm password regex that contains above requirements and it is also equal to my password string. Could anyone can help me?

Chris So
  • 711
  • 1
  • 11
  • 23
  • That `getRegularExpression:` is an extremely confusing an un-idiomatic name (in Objective-C, `get...` functions are **not** getters); furthermore, there's no much point in creating a method that just returns the very same constant string always. – The Paramagnetic Croissant May 01 '14 at 07:01

4 Answers4

1

In your shouldChangeCharactersInRange you can do it by this

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^$|^[^[:space:]]{6,20}$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression 
                                                                           options:NSRegularExpressionCaseInsensitive 
                                                                            error:nil];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];        
    if (numberOfMatches == 0)
        return NO;        


    return YES;
}
Shruti
  • 1,849
  • 1
  • 13
  • 21
  • Thanks for answering @Shruti I know how to check my password via this call back. however, I don't know how to embed the password string into regex to validate the confirm password whether they are equal.... – Chris So May 01 '14 at 06:57
  • to check confirm password and password are equal or not you can check by `isEqualToString` for the text of two textfields. I hope i am getting your question correctly. – Shruti May 01 '14 at 07:10
1

You can use NSPredicate.

NSString *passwordRegex = @"^$|^[^[:space:]]{6,20}$";

NSPredicate *password = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
if([password evaluateWithObject:@"YOUR PASSWORD"){
    //success
}
Sreejith
  • 1,345
  • 13
  • 25
0

If you want to see if both your regex qualifies as well as whether the password matches the original password, you generally would not do the latter via regex. You should just check for the string equality with NSString method isEqualToString.:

- (BOOL)validateString:(NSString *)input password:(NSString *)password
{
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^$|^[^[:space:]]{6,20}$" options:0 error:&error];

    if ([regex numberOfMatchesInString:input options:0 range:NSMakeRange(0, [password length])] > 0 && [input isEqualToString:password]) {
        return YES;
    }

    return NO;
}

Having said that, I would not recommend checking both the regex and the string equality. And one of two situations will arise: Either they're inconsistent (because the password you're validating against doesn't, itself, pass the regex) or they're redundant (because if the password you're validating against satisfies the regex, then all you need to check is the equality of the strings).

Generally, I'd do some validation, such as your regex, for new passwords, but when validating against another password, I'd just check for equality only.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

Expression for match those constraint and password:

- (NSString *) getRegularExpression: (NSString *) extraWords
{
    /*
     *  ^$              Empty string
     *  |               Or
     *  ^               Start of a string
     *  []              Explicit set of characters to match
     *  ^[:space:]      Matches any non-white-space character - equivalent to \S
     *  {6,30}          6-20 characters
     *  $               End of a string
     *  (?=...)(?=...)  And Operators
     */
     //    eg: @"^$|(?=^[^[:space:]]{6,20}$)(?=^password$)"
    return [NSString stringWithFormat:@"^$|(?=^[^[:space:]]{6,20}$)(?=^%@$)", extraWords];
}

extraWords is for receiving password string.

Chung Ngok
  • 24
  • 1
  • 1
    No, this answer is not right. What if the password itself contained some characters that were regex? For example, what if the `extraWords` was `@"......"` (i.e. six periods). Then the resulting regex test would succeed with any six letters passed to it. – Rob May 01 '14 at 08:41