Pergunta

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?

Foi útil?

Solução 3

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.

Outras dicas

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;
}

You can use NSPredicate.

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

NSPredicate *password = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
if([password evaluateWithObject:@"YOUR PASSWORD"){
    //success
}

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top