I am trying to validate conf开发者_JS百科irm passwords in PHP using JavaScript by this code:
if($_POST['PasswordField']== $_POST['ConfirmPassword'] && $_POST['PasswordField']>='8')
{
echo "Succed <br>";
}
else
{
echo "filed <br>";
}
It works well with matching the two passwords, but the length of the password is not working. But if I enter a password which is less than 8 character it succeeds - why is this?
Also, how can I check password strength using JavaScript but not using Regular expressions?
You should use the strlen() method to get the length of the string that is contained in $_POST['PasswordField']. And you should not check it with a string '8'. So it needs to be like:
<?php
function isPasswordValid($password1, $password2){
if(strlen($password1) >= 8)
if($password1 == $password2)
return true;
return false;
}
?>
Call this method with the two values from your POST. Also, use trim() to strip whitespaces. Ohw...and it has nothing to do with JavaScript.
For password length you need to use strlen
...&& strlen($_POST['PasswordField']) >= 8)...
if($_POST['PasswordField']== $_POST['ConfirmPassword'] && strlen($_POST['PasswordField'])>8 && $_POST['PasswordField']>='8')
{
echo "Succeed <br>";
}
else
{
echo "failed<br>";
}
You need the strlen();
function to get the length of the password.
http://php.net/manual/en/function.strlen.php
if (strlen($_POST['PassWordField']) > 8){
//do stuff
}
精彩评论