Reputation: 1083
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!
Upvotes: 0
Views: 21279
Reputation: 108
What you want to do is escape each of "
, '
, and ,
using a \
. The regex you're looking for is:
/([\^\`\~\<\,\>\"\'])/
I actually generated that using the JSVerbalExpressions library. I highly recommend you check it out! To show you how awesome it is, the code to generate the above regex is:
var tester = VerEx()
.anyOf("^,`'\"~<>");
console.log(tester); // /([\^\`\~\<\,\>\"\'])/
Upvotes: 2
Reputation: 6894
Include these special characters in square brackets without commas and see if it works.
You can try it out here - http://jsfiddle.net/BCn7h/
Eg :
if (password.match(/["',]/)
{
strength -= 2
}
Upvotes: 1
Reputation: 13013
You don't need to separate your individual characters by commas, nor do you need to wrap the only term in brackets.
This should work:
/[`^~<>,"']/
note the carat (^
is not at the front, this has a special meaning when placed at the start of the []
block)
Also you should use test()
because you only want a boolean if-contains result
/[`^~<>,"']/.test(password)
Upvotes: 2