Reputation: 23
Here is my regex for validating a username:
^([a-zA-Z0-9]+([\\s.]?[a-zA-Z0-9]+)*){6,20}$
The username should be between 6-20
characters, with space
or period(.)
followed by a word or character.
But this regex fails for the following examples, even though the entire string length(including space is at least 6 characters):
Any help will be appreciated!
Upvotes: 0
Views: 114
Reputation: 119
You can match your user with this.
^([a-zA-Z0-9\s\.]){6,20}$
This will probably match more than just your login. Because this will match multiple blank spaces or periode. You can use an other regex to mach that you login has just one periode or blank space.
^[a-zA-Z0-9]+[\s\.]?[a-zA-Z0-9]+$
Hope it helps.
Upvotes: 0
Reputation: 71538
You could use a positive lookahead to make sure there's between 6 to 20 characters inclusive:
^(?=.{6,20}$)[a-zA-Z0-9]+([\\s.][a-zA-Z0-9]+)*$
^----------^
And you shouldn't really need the ?
for the space/period here.
Upvotes: 1