I am creating a username field with Laravel and I'm using RegEx to set the formatting rules.
The rule is: Not only numbers **AND OR** underlines only numbers, chars, and underlines are allowed in general.
Now I came up with this RegEx syntax:
(?!^\d $)^.[a-zA-Z0-9_] $
This works fine until the point where I input stuff like:
____2348734
How can I prevent that? Thanks in advance!
CodePudding user response:
You can use either of
^(?![\d_] $)[a-zA-Z0-9_] $
^(?![\d_] $)\w $
See the regex demo. Also, consider using \z
instead of $
to disallow a trailing newline input.
Details:
^
- start of string(?![\d_] $)
- the string cannot only contain digits / underscores[a-zA-Z0-9_]
/\w
- one or more alphanumeric or_
chars$
- end of string. (\z
will match the very end of string.)