Search code examples
androidregexkotlinvalidationpasswords

password validation no Character sequence ( kotlin android studio )


I have the task to create a password validation that has to consider some things. The only problem I have is that one of the criteria of the password validation is that the password must not contain any sequences, e.g. (12345), (abcdef), (asdfghjk). I have already searched a lot and do not know how to implement this. Can anyone help.


Solution

  • Since you didn't give much detail into what code you already have and what you're stuck on about the logic, here's a very generalized description of a strategy you could use to do this:

    1. Create a List<Iterable<Char>> that contains all the possible strings of characters that could be considered a range. For example:
        val charRuns = listOf(
            '0'..'9',
            'a'..'z',
            'A'..'Z',
            "qwertyuiop".asIterable(),
            //...
        )
    
    1. Iterate these runs to fill a MutableMap<Char, MutableSet<Char>>, where the keys are any of the characters from the runs, and the values are sets of all the chars that if they appear next in a string should be considered a consecutive sequence.

    2. Iterate the potential password String, using the map to check the subsequent Char of each Char to see if it should be considered part of a sequence. Use a counter variable to count the current size of sequence found so far, and reset it whenever a non-sequence is found. If it ever rises above your threshold for allowable sequence size, reject the password immediately.