1

I need a regular expression that can validate following cases,

  • Be 8 to 20 characters
  • Use at least 1 upper case letter, 1 lower case letter, and 1 number
  • Not repeat the same number or letter more than 3 times in a row
  • Not contain spaces, and may only use these characters @ # * ( ) + = { } / ? ~ ; , . - _

The closest solution I can find is

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z])(?=.*[@(#*){+=}/?~;,._]).{8,20}$

But contains following issue,

  • Accepting space
  • Can't add - character
  • Repeating same number or letter more than 3 times in a row.

EDIT: Fixed the repeating character issues and have final expression like,

^(?!.*?(.)\1{3})(?!.* )(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?!.*[:£€&"!'[\]%^\|<>$]).{8,20}$ 

But its only working on online tester like [RegExr][1]
I tried following code but it generates runtime

- (BOOL)string:(NSString *)text matches:(NSString *)pattern{

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];

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

return matches.count > 0;

}

iEngineer
  • 1,319
  • 1
  • 11
  • 27
  • Possible duplicate of [Regular Expression for Password Validation Objective-C](http://stackoverflow.com/questions/26423427/regular-expression-for-password-validation-objective-c) – Abhi Apr 07 '17 at 06:34
  • have you tried to write some code for those requirements ? – vaibhav Apr 07 '17 at 06:37
  • @Abhi No its not. Please read both of the questions and then mark as duplicate. I have mentioned my issues in detail. – iEngineer Apr 07 '17 at 06:37
  • @vaibhav I mentioned my code in question. The regular expression that is bit close to my solultion. – iEngineer Apr 07 '17 at 06:39
  • what do you mean by Repeating same number or letter? – karthikeyan Apr 07 '17 at 06:41
  • @iEngineer, your code contains character-set only without logic which doesn't count's for solution attempt. – vaibhav Apr 07 '17 at 06:43
  • @karthikeyan I want user to not repeat same letter or number more than 3 times in a row like abbbb5Xxxxx66666. – iEngineer Apr 07 '17 at 06:44
  • @vaibhav I know the logic I just need regular expression that can validate these requirements. Simple. – iEngineer Apr 07 '17 at 06:45
  • If you say so then its okay but here just look at your ques asking too much or broad, share specific problem with tried code. – vaibhav Apr 07 '17 at 06:48
  • many solutions you can try and combine those ...http://stackoverflow.com/questions/29535792/check-if-a-string-contains-at-least-a-uppercase-letter-a-digit-or-a-speical-ch, http://stackoverflow.com/questions/6730435/check-whether-an-nsstring-contains-a-special-character-and-a-digit. – vaibhav Apr 07 '17 at 06:52
  • 1
    ^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,} to avoid spaces, if your string have space, it will show you alert or – karthikeyan Apr 07 '17 at 07:19
  • @iEngineer Try http://ideone.com/CYbNTg. Please confirm it works as expected. I understand those special chars are not required. [Here is the regex demo](https://regex101.com/r/Il9bkR/1). – Wiktor Stribiżew Aug 03 '17 at 21:55

0 Answers0