I have a signup page for my website.
When the user fills in the form, they get real-time validation through a JavaScript Regex, which works fine. This is done through:
var password = document.getElementsByName("password")[0].value;
var pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)([a-zA-Z0-9!\"\#$%&\'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~]{6,})$/;
if(pattern.test(password)){
document.getElementById("check_password").innerHTML = "Password is valid.";
} else {
document.getElementById("check_password").innerHTML = "Password is invalid. It should have at least 6 characters, and 1 lowercase letter, uppercase letter, and number.";
}
The PHP Regex is used when the user submits the form through this:
if(!preg_match("/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)([a-zA-Z0-9!\"\#$%&\'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~]{6,})$/", $_POST["password"])){
$error = "Password is invalid. It should have at least 6 characters, and 1 lowercase letter, uppercase letter, and number.";
}
However, the PHP keeps throwing up the error even when the JavaScript doesn't. Both regexes are the same and have been tested here in the PHP setting and JavaScript setting. Yet it works in JavaScript but not PHP!
Why does it not work in PHP, and how can I solve the problem?