Reputation: 931
I've written a function to validate if a password is valid. The only problem I'm having is figuring out why this pattern that I've written in JavaScript isn't evaluating to true when tested with a password such as: 'SteveRogers#256'. Is this an issue with the way I've declared the regex pattern?
PHP
function check_password($pass_word)
{
$pattern = "#.*^(?=.{8,15})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$#";
return (preg_match($pattern, $pass_word));
}
JavaScript
function check_password(pass_word) {
var pattern = new RegExp("#.*^(?=.{8,15})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$#");
return pattern.test(pass_word);
}
Upvotes: 1
Views: 81
Reputation: 785481
Remove regex delimiters in Javascript as new RegExp
takes a String in the constructor. Correct Javascript code should be:
var pattern = new RegExp("^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?\\W).{8,15}$");
Upvotes: 3