jay jay - 5 months ago 37
Javascript Question

Validate nickname with regex

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

Answer

your regex:

^(^[^0-9])([\w a-z A-Z 0-9][^@#])$

explained

  1. dont start with a number.start with any thing other than numbers.

  2. 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