Regex for special characters

tonelm54
tonelm54 used Ask the Experts™
on
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(/(.*[!,%,&,@,#,$,^,*,?,{,},|,(,),[,],_,-,/,\])/)) {
  	$("#tdContainsSymbol").html("P");
  } else {
  	$("#tdContainsSymbol").html("O");
  }
});

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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Guru
Most Valuable Expert 2011
Commented:
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

Commented:
I tested the pattern here: https://jsfiddle.net/7as3wu8o/

Author

Commented:
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