Validation of a Password - Python

前端 未结 11 2000
自闭症患者
自闭症患者 2020-11-27 08:20

So I have to create code that validate whether a password:

  • Is at least 8 characters long
  • Contains at least 1 number
  • Contains at least 1
11条回答
  •  猫巷女王i
    2020-11-27 08:41

    password.isdigit() does not check if the password contains a digit, it checks all the characters according to:

    str.isdigit(): Return true if all characters in the string are digits and there is at least one character, false otherwise.

    password.isupper() does not check if the password has a capital in it, it checks all the characters according to:

    str.isupper(): Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

    For a solution, please check the question and accepted answer at check if a string contains a number.

    You can build your own hasNumbers()-function (Copied from linked question):

    def hasNumbers(inputString):
        return any(char.isdigit() for char in inputString)
    

    and a hasUpper()-function:

    def hasUpper(inputString):
        return any(char.isupper() for char in inputString)
    

提交回复
热议问题