Regex for special characters

tonelm54 used Ask the Experts™
Im trying to find out if a textbox for a password contains a special character, so written the following code:-
$("#txtPassword").on("keyup", function () {
	if ($("#txtPassword").val().match(/(.*[!,%,&,@,#,$,^,*,?,{,},|,(,),[,],_,-,/,\])/)) {
  } else {

Open in new window

However its saying there is an issue with the regex command, Im assuming its to do with escape characters so changed the characters like []|\ and put a \ as an escape character, but didnt work.

Any ideas?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Guru
Most Valuable Expert 2011
Try this pattern:

Open in new window

The following characters definitely need escaping, as they have special meaning (let me know if you want an explanation):

The / character also should probably be escaped, and just in case I've also escaped the round and curly bracket characters.

The commas are not used for delimiting, so I've just left one in there.
Terry WoodsIT Guru
Most Valuable Expert 2011

I tested the pattern here:


Excellent, worked great :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial