Friday, 23 August 2013

Do not allow special characters except the allowed characters javascript regex

Do not allow special characters except the allowed characters javascript regex References I have the following javascript code for my password strength indicator: if (password.match(/([!,@,#,$,%])/) { strength += 2 } So what this do is if the password contains one of these allowed characters (!,@,#,$,%), it will add a value to the strength of indicator. My problem is I also want to decrease the strength of the password indicator once other special characters are present on the password. For example: ^,`,~,<,> To remove confusion, basically I don\'t want any other special characters except the ones that is present above (!,@,#,$,%). So I did it hard coded, writing all special characters that I don\'t want. I tried using this: if (password.match(/([^,`,~,<,>])/) { strength -= 2 } But I also don\'t want to include \", \' and , but then if I include them on my if condition, it will throw me an error saying syntax error on regular expression. I understand this because i know \" represents a string which must be closed. Can I do something about it? Thanks in advance!

No comments:

Post a Comment