1

I want to reject a username or password string that has minimal 4 chars, maximum 39 chars, with a-z, A-Z, 0-9, -, _, @ and . and that the characters should be not-repeating and not-incremental like 'aaaa' or '1234' or 'abcd' and URL (HTTP-POST) friendly.

I've been trying to look into String.matches().

The part minimal 4 chars, maximum 39 chars, with a-z, A-Z, 0-9 doesn't seem to difficult, but is there a way to test a string on a match with all of these requirements?

3
  • I recommend using a regex to just validate the length and the allowed characters, and then using a custom Java function that scans for repeated or incrementing characters if the input passes the regex. There is no way for regexes to recognize incrementing characters except by listing all possible strings with incrementing characters. Jul 30, 2013 at 3:12
  • 1
    What do you mean by “URL (HTTP-POST) friendly”? Isn’t that just based on what characters are allowed, which you’ve already listed? Anyway, it would be better to not restrict the username based on URL-friendliness, and instead call a URL-encoding function (that changes special characters to entities like %xx) before generating a URL, and a URL-decoding function when interpreting the URL. Jul 30, 2013 at 3:15
  • You cannot do that non-incremental thing. Regex is not for that.
    – d'alar'cop
    Jul 30, 2013 at 12:55

1 Answer 1

0

I would validate all those things separately. That will make all validations simpler, easier to understand and maintain.

Plus you will know which constraint is being violated, and so you can report a more accurate error to the user (e.g. "password too short" instead of "your password should validate the following constraints: ....")

  • length: using String.length()
  • allowed chars: using a regex : "^[a-zA-Z0-9_@\\.-]*$"
  • non-repeating and non-incremental: regexes are not meant for that. As mentioned in a comment, I would go for a custom validation (iterate over chars of input, detect constant and incrementing sequences of char). Something like that should work:

    public boolean isBad(String test) {
        int repeating = 0;
        int incremental = 0;
        for (int i = 1; i < test.length(); ++i) {
            char prev = test.charAt(i - 1);
            char curr = test.charAt(i);
            if (prev == curr) {
                repeating++;
            } else {
                repeating = 0;
            }
            if (curr - prev == 1) {
                incremental++;
            } else {
                incremental = 0;
            }
            if (repeating == 3) {
                System.out.println("too many repeated " + curr);
                return true;
            }
            if(incremental==3){
                System.out.println("incrementing sequence");
                return true;
            }
        }
        return false;
    }
    
  • url-friendly: the limit you put on the character set (alphanumeric and a few symbols) should be sufficient

Not the answer you're looking for? Browse other questions tagged or ask your own question.