Password validation in UITextField in iOS

前端 未结 11 1113
有刺的猬
有刺的猬 2020-12-01 12:54

I have 1 UITextfield for password in my iPhone application.

I want to validate this textfield with the following validation.

  • Must be at le
11条回答
  •  青春惊慌失措
    2020-12-01 13:29

    for me Best way was to use NSPredicate and regex. this is regex for your case: ^(?=.{10,})(?=.*[0-9])(?=.*[a-zA-Z])([@#$%^&=a-zA-Z0-9_-]+)$

    objective C code:

    NSString *regex = @"^(?=.{10,})(?=.*[0-9])(?=.*[a-zA-Z])([@#$%^&=a-zA-Z0-9_-]+)$";
    
    NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isValid = [passwordTest evaluateWithObject:yourTextfield.text];
    

提交回复
热议问题