iOS Password Regular Expression

Requirements: must contain uppercase letters + lowercase letters + arrays + symbols

NSString * psw = @"Abc123456@!";

NSString * regex1 = @"^[0-9]{8,16}$|^[a-zA-Z]{8,20}$";

NSString * regex2 = @"^[A-Za-z0-9]{8,20}$";

NSString * regex3 = @"^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$";

Note: \d in the expression of regular 3 will be regarded as an escape character in iOS, so it needs to be written as \\d, so if you use this regular expression in other languages, remember to remove \

NSPredicate * pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];

NSPredicate * pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2];

NSPredicate * pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex3];

BOOL result1 = [pred1 evaluateWithObject:psw];

BOOL result2 = [pred2 evaluateWithObject:psw];

BOOL result3 = [pred3 evaluateWithObject:psw];

password is true if result1 and result12 fail and result3 passes

  if (result1 ==NO && result2 ==NO && result3 ==YES)  {

         //Password passed, write your logic here

   }else{

         password failed

  }

Guess you like

Origin blog.csdn.net/wyz670083956/article/details/125096945