Regular Expression In Android for Password Field

前端 未结 10 1154
执念已碎
执念已碎 2020-12-13 09:56

How can i validating the EditText with Regex by allowing particular characters . My condition is :

Password Rule:

  1. On

10条回答
  •  难免孤独
    2020-12-13 10:24

    Most common password validation is

    1. At least 8 character
    2. Require numbers
    3. Require special character
    4. Require uppercase letters
    5. Require lowercase letters

    Regex:

    ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\\\/%§"&“|`´}{°><:.;#')(@_$"!?*=^-]).{8,}$
    

    Kotlin code:

        val PASSWORD_REGEX_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\\\/%§"&“|`´}{°><:.;#')(@_$"!?*=^-]).{8,}$"
    
        fun isValidPassword(password: String?): Boolean {
            val pattern: Pattern =
                Pattern.compile(PASSWORD_REGEX_PATTERN)
            val matcher: Matcher = pattern.matcher(password)
            return matcher.matches()
        }
    

    online regex validator to check it:

    • https://regex101.com/
    • https://www.freeformatter.com/java-regex-tester.html#ad-output

提交回复
热议问题