Home > Net >  How do we make sure password is strong? and How to validate when user put the password
How do we make sure password is strong? and How to validate when user put the password

Time:04-26

  1. Password should contain at least one char, One digit, One Capital letter, and One special char.

  2. Password should not have a sequence of char or digits.

  3. Password should not same as username?

How can we validate all things in swift or Objective or Java?

CodePudding user response:

This is Regex

(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ =_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*ˆ =_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ =_]))[A-Za-z0-9-!@#$%&*ˆ =_]{6,15}

Explanation For Regex

 ^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[!@#$&*])            Ensure string has one special case letter.
(?=.*[0-9].*[0-9])        Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8}                      Ensure string is of length 8.
$                         End anchor.

function to use this Regex

public func isValidPassword() -> Bool {
let passwordRegex = "(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ =_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*ˆ =_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ =_]))[A-Za-z0-9-!@#$%&*ˆ =_]{6,15}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
}

Use Like This

**//Example 1**

 var password = "@Abcdef011" //string from UITextField (Password)
  password.isValidPassword() // -> true

**//Example 2**

 var password = "Abcdef011" //string from UITextField 
 password.isValidPassword() // -> false

CodePudding user response:

For the 3rd case, in Java would be like this:

String username = wherever you get the username; String pwd = wherever you get the password;

if(username.equals(pwd) {
   do somehting;
} else {
   do something too;
}

Maybe you want to wrap that into a function that returns true if the password is different. It would be more clear to read.

  • Related