-2

Can anyone Please Help me i need a regular expression Password Validation.

Condition : Password should be maximum of 8 characters numeric or alphabetic with atleast one special characters

Thanks in advance.

vikingosegundo
  • 51,126
  • 14
  • 131
  • 172
Suresh Peddisetti
  • 3,634
  • 3
  • 21
  • 26

3 Answers3

5
^(?=.{2,8}$)(?=.*?[A-Za-z0-9])(?=.*?[\W_])[\w\W]+

This regular expression allowes 2 to 8 character passwords. It requires to have at least 1 alphanumeric (letter/number) character and 1 non-alphanumeric character. You can test it here.

If you want to change the minimum characters required or higher the maximum. Just change the 2 or 8.

Xyzer
  • 111
  • 7
3

SOLUTION :

-(BOOL)isValidPassword:(NSString *)checkString{
NSString *stricterFilterString = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$#!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stricterFilterString];
return [passwordTest evaluateWithObject:checkString];
}

TEST :

 NSLog(@"%@", [self isValidPassword:@"D33p@k$235#@df"] ? @"YES" : @"NO");

OUTPUT :

YES
Deepak Kumar
  • 945
  • 9
  • 18
1

Swift 3

check if the password lenght more than or equal 8 and have lowercase , uppercase ,decimalDigits and special characters like !@#$%^&*()_-+ is optional.

Why i not use regular expression ?

Because it's difficult to support reserved characters in regular expression syntax.


func isValidated(_ password: String) -> Bool {
    var lowerCaseLetter: Bool = false
    var upperCaseLetter: Bool = false
    var digit: Bool = false
    var specialCharacter: Bool = false

    if password.characters.count  >= 8 {
        for char in password.unicodeScalars {
            if !lowerCaseLetter {
                lowerCaseLetter = CharacterSet.lowercaseLetters.contains(char)
            }
            if !upperCaseLetter {
                upperCaseLetter = CharacterSet.uppercaseLetters.contains(char)
            }
            if !digit {
                digit = CharacterSet.decimalDigits.contains(char)
            }
            if !specialCharacter {
                specialCharacter = CharacterSet.punctuationCharacters.contains(char)
            }
        }
        if specialCharacter || (digit && lowerCaseLetter && upperCaseLetter) {
            //do what u want
            return true
        }
        else {
            return false
        }
    }
    return false
}
let isVaildPass:Bool = isValidated("Test**00+-")
print(isVaildPass)
Community
  • 1
  • 1
dimohamdy
  • 2,421
  • 25
  • 21