1

I want a client side validation of userid using javascript. Sample inputs are

  1. !abc
  2. abc-def , abc
  3. abc , abc-def
  4. abc
  5. 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.

2
  • 3
    Did you start your regex with ^ sign and end with $ ? Otherwise it will not match whole string. Commented Mar 19, 2013 at 10:25
  • Thank you.It worked. Post it as an answer. Commented Mar 19, 2013 at 10:39

2 Answers 2

1

Your problem is \w. try using a-zA-Z_ instead. this makes sure that only alphabet is used

Sign up to request clarification or add additional context in comments.

Comments

0

\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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.