0

I've a regex check for my email parameter, I want to implement regex check as well for password parameter.

For my email account I do something like this:

- (BOOL)isValidEmailAddress:(NSString*)emailAddressToCheck
{
    BOOL stricterFilter = YES;
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emailAddressToCheck];
}

But I wonder how can I do it for password parameter check:

  1. What I want to achieve is password with repeated character more than 3 times is not allowed.
  2. 1, 2, 3 OR 3, 2, 1 for example is not allowed.
  3. Letters and numbers.

Any ideas how can I do that?

1 Answer 1

3

Idan, as a regex lover, I really hate to say that something is not possible with regex. However, I don't think that the task you specified is a good task for regex. True, someone could conceivably find a way to hack a solution together, I've been blown away before. That being said, I have never seen a solution for point 2 below.

  1. For instance, to check that each character, a for instance, occurs less than 3 times in the string, is quite challenging and probably involves an infinite-width lookaround, available only in .NET

  2. Regex is of no use when it comes to checking random sequences such as abc, 123, 987

  3. As I'm sure you're aware, number 3 is not a problem. In a unicode environment, where \d could stand for Arabic digits, you are better off sticking with specific character classes, such as [a-z0-9] in case-insensitive mode.

I would make separate methods for each criterion and forget about regex, except of course as a pre-filter for simpler conditions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.