0

I have a regex that makes sure the username has only characters from a-z or A-Z or 0-9 and is between 3-15 characters.

What I'd like a separate regex for each:

1) Check if its between 3-15 characters (any symbol any alphabet; only thing that matters is no.of characters)

2) Must contain characters from a-z or A-Z or 0-9 or . _ (dot and underscore) (no space/whitespace)

3) Characters from a-z or A-Z (can contain spaces/whitespace)

This works fine:

private static final String PASSWORD_PATTERN = "^[a-zA-Z0-9_-]{3,15}$";
etlogin_username.addValidator(new RegexpValidator("Regex wrong", USERNAME_PATTERN));

The following returns false:

1) USERNAME_PATTERN1 returns false for "dummyu!:)"

2) REALNAME_PATTERN returns false for "Davic Schummer"

private static final String USERNAME_PATTERN1 = "^\\w{3,15}$";
private static final String USERNAME_PATTERN2 = "^[a-zA-Z0-9_-]{3,15}$";

private static final String REALNAME_PATTERN = "^[a-zA-Z]$";

etlogin_username.addValidator(new RegexpValidator("Must be between 3-15 characters", USERNAME_PATTERN1));
etlogin_username.addValidator(new RegexpValidator("Symbols not allowed", USERNAME_PATTERN2));

etlogin_realname.addValidator(new RegexpValidator("Only alphabets allowed", REALNAME_PATTERN));

NOTE: I know how to user String.length ; but I'd like a regex expression for it as I can pass it to the Validator, which takes care of a lot of other things

Edit1: Added regex validator:

public class RegexpValidator extends METValidator {

  private Pattern pattern;

  public RegexpValidator(@NonNull String errorMessage, @NonNull String regex) {
    super(errorMessage);
    pattern = Pattern.compile(regex);
  }

  public RegexpValidator(@NonNull String errorMessage, @NonNull Pattern pattern) {
    super(errorMessage);
    this.pattern = pattern;
  }

  @Override
  public boolean isValid(@NonNull CharSequence text, boolean isEmpty) {
    return pattern.matcher(text).matches();
  }
}
Zen
  • 2,524
  • 3
  • 19
  • 40
  • 2
    Is the pattern anchored in `RegexpValidator`? If no, try `"[a-zA-Z0-9._]"` for 2) and `"[a-zA-Z\\s]"` for 3). Also, to check the length regardless of the character type, I guess you'd better use `"^.{3,15}$"` for 1). – Wiktor Stribiżew Jun 14 '16 at 06:58
  • The validator for REALNAME returns false for both: "david schummer" and "davidschummer" ; 1 & 3 work fine – Zen Jun 14 '16 at 07:10
  • That does not sound consistent: [the second regex should find a partial match in `david` and `david moretexthere`](https://regex101.com/r/fQ2aC5/1) – Wiktor Stribiżew Jun 14 '16 at 07:12
  • should I add ^ and $ ? – Zen Jun 14 '16 at 07:13
  • No, since with `"[a-zA-Z0-9._]"`, you are looking for a partial match. If you need the whole string match, use `"^.*[a-zA-Z0-9._].*$"` – Wiktor Stribiżew Jun 14 '16 at 07:17
  • This is the regex I used for REALNAME '"^[\\p{L} .'-]+$"' ; the other two work appropriately – Zen Jun 14 '16 at 07:23
  • So, the `.matches()` method is used. You need to use regexps that match full strings. – Wiktor Stribiżew Jun 14 '16 at 07:26
  • Im not familiar with how regex works; I've posted the regex used. Will they work fine or would they break under a certain instance? – Zen Jun 14 '16 at 07:28
  • I do not know as your regex `^[\\p{L} .'-]+$` that matches 1 or more Unicode letters (not allowing diacritics), a space, a `.`, a `'` or a `-` does not match your specs (*Must contain characters from `a-z` or `A-Z` or `0-9` or `.` `_`(dot and underscore) (no space/whitespace)*). – Wiktor Stribiżew Jun 14 '16 at 07:48
  • On a side note: your constructor taking a `String` should call the constructor taking a `Pattern` like so: `RegexpValidator(String m, String r) { this(m, Pattern.compile(r)); }` for better maintainability. – Good Night Nerd Pride Jun 14 '16 at 07:58
  • @WiktorStribiżew I came across that one later and there was a good reason given by the person posting the answer. But the one provided by you still gives an error for Type 3. Type 1 & Type 2 work fine – Zen Jun 14 '16 at 08:16
  • 1
    What you say is contradictory. Type 1: `"^.{3,15}$"` - should work alright with `.matches()`. Type 2: `"^[a-zA-Z0-9._]+$` matches 1+ chars you specified and will work with `matches()`. Type 3: `"[a-zA-Z\\s]"` *can't* work as you need a regex matching the *whole* string. Use `"^.*[a-zA-Z\\s].*$"` – Wiktor Stribiżew Jun 14 '16 at 08:20
  • But this works fine in my case: "^[\\p{L} .'-]+$" or if going by my questions even this should work: "^[\\p{L}]+$" – Zen Jun 14 '16 at 08:28

2 Answers2

0

Try this

public class UsernameValidator{

          private Pattern pattern;
          private Matcher matcher;

          private static final String USERNAME_PATTERN = "^[a-z0-9_-]{3,15}$";

          public UsernameValidator(){
              pattern = Pattern.compile(USERNAME_PATTERN);
          }

          /**
           * Validate username with regular expression
           * @param username username for validation
           * @return true valid username, false invalid username
           */
          public boolean validate(final String username){

              matcher = pattern.matcher(username);
              return matcher.matches();

          }
    }
Divyesh Boda
  • 266
  • 2
  • 12
0

Regex used:

private static final String PASSWORD_PATTERN = "^.{3,16}$";

private static final String USERNAME_PATTERN1 = "^.{3,16}$";
private static final String USERNAME_PATTERN2 = "^[a-zA-Z0-9._-]$";

private static final String REALNAME_PATTERN1 =  "^.{3,36}$";
private static final String REALNAME_PATTERN2 =  "^[\\p{L} .'-]+$";
Zen
  • 2,524
  • 3
  • 19
  • 40
  • The explanation for regex REALNAME_PATTERN2 can be found here: http://stackoverflow.com/questions/15805555/java-regex-to-validate-full-name-allow-only-spaces-and-letters – Zen Jun 14 '16 at 07:27