Regular Expression In Android for Password Field

前端 未结 10 1103
执念已碎
执念已碎 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:06

    All of the other answers are good, but the implementation of special characters were a bit too messy for my taste. I used Unicode for special characters instead.

    I used special characters specified in the OWASP website.

    Kotlin:

    val SPECIAL_CHARACTERS_REGEX =
        "?=.*[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E]"
    val PASSWORD_REGEX =
        "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])($SPECIAL_CHARACTERS_REGEX).{8,}\$"
    
    fun isValidPassword(password: String) = Pattern.matches(PASSWORD_REGEX, password)
    

提交回复
热议问题