I’m trying to work on a regex js username checker for registration, however I’m having a few issues
This is the expression I wrote
^[a-zA-Z]{1}([a-zA-Z0-9]*[\._ ]?){2,10}[A-za-z\d]$
The problem is that it doesn’t limit the period, underscore, and spaces, and also the username should be between 4-12 characters.
I’m trying to allow:
First character has to start with A-Z a-z
Last character has to be alphanumeric
Everything inbetween must be 2-10 alphanumeric characters and allowing only 1 space, period and underscore.
For example:
- A___N is not valid (3 undescores)
- A_NON is valid
- “A.Non ” is not valid, space at the end.
- A.non is valid
- A N.oN is valid
Any help would be appreciated
The best approach is to use a lookahead assertion to combine two unrelated requirements. Essentially, you need ensure that this regex does match:
and that this one does not match:
You can combine them by using a negative lookahead assertion: