Reputation: 29
I want a client side validation of userid using javascript. Sample inputs are
!abc
abc-def , abc
abc , abc-def
abc
abc-def_hjk
I have made a regex ([\w]*[-]?[\w]+[\s]?[,]?[\s]?)+
. It matches 2,3,4,5
as needed but also matches input 1
, which is invalid.
Please let me know what is wrong in this regex.
Upvotes: 1
Views: 840
Reputation: 45
Your problem is \w
. try using a-zA-Z_
instead. this makes sure that only alphabet is used
Upvotes: 1
Reputation: 172
\d, \w and \s Shorthand character classes matching digits, word characters (letters, digits, and underscores), and whitespace (spaces, tabs, and line breaks). Can be used inside and outside character classes. \D, \W and \S Negated versions of the above. Should be used only outside character classes. (Can be used inside, but that is confusing.)
Upvotes: 0