简体   繁体   中英

Form Validation With Regex

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);
}

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}$");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM