I neet to validate a nick name without @
and #
and it can't start with numbers
thi is my solution but the @
and #
(^[^0-9])([\w a-z A-Z 0-9][^@#])
.test:
andrea //true
0andrea // false
// these are true but I need them to be false
and@rea // true
and#rea // true
your regex:
^(^[^0-9])([\w a-z A-Z 0-9][^@#])$
explained
dont start with a number.start with any thing other than numbers.
two letter upper/lower case or digit or underscore followed by any of ^@#
SO according to your regex, any three letter word will be matched.
use this:
^[^0-9]\w+$
this will work for usernames not conatining any special chars.
If you specifically just not want @ and #. use this :
^[^0-9][^@#]+$
demo here : https://regex101.com/r/gV9eO0/1
I neet to validate a nick name without
@
and#
and it can't start with numbers
Using (!?pattern)
is a negative lookahead so you can prevent a \d
as first char without preventing the rest of the expression from looking at it, then
If you're only blacklisting then
^(?!\d)[^@#]+$
Otherwise, use a whitelist
^(?!\d)[a-zA-z\d ]+$
Notice how we keep matching to the end of the string, $
.
I've used +
because you probably don't want to permit zero-length nicknames.
Also, you can sort-of go down the route you were attempting by using a *
or +
operator on a group;
^(?!\d)(?:(?![@#])[a-zA-Z\d ])+$
Graphical representations via debuggex.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With