在PHP中验证密码和确认密码

问题描述 投票:1回答:2

我正在尝试一个注册表单,其中密码和确认密码应该相互匹配。如果不匹配则应显示错误消息。以及如何验证密码超过6个字符?我怎样才能做到这一点?如何验证密码和确认密码匹配并显示错误信息密码错误?

这是代码

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 
$db = "reg";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db($db,$dbhandle) or die('cannot select db');

if(isset($_POST['submit'])) {

     $name= $_POST['name'];

     $username= $_POST['username'];
    $password= $_POST['password'];
     $cpassword= $_POST['cpassword'];

     if($name==''){
     echo "<script>alert('Please enter Name')
     </script>";
    exit();
    }
    if($username==''){
     echo "<script>alert('Please enter Username')
     </script>";
    exit();
    }
    if($password=='' && $password<6){
     echo "<script>alert('Please enter  Password')</script>";

    exit();
    }

    if($cpassword==''){
     echo "<script>alert('Please enter Confirm Password')
     </script>";

    }



    $check_name="select * from registration where username='$username'";
    $run=mysql_query($check_name);

     if(mysql_num_rows($run)>0){
    echo "<script>alert('Username $username already exits in our database. Please try with Another!')</script>";

    }

    elseif($password != $cpassword){
       echo "<script>alert('passwords doesn't match')</script>";
    }
    else{
          $query = "INSERT INTO `registration` (name,username, password,cpassword) VALUES ('$name','$username', '$password', '$cpassword')";
          $run1=mysql_query($query);

    if($run1){
    echo "<script>window.open('register.html','_self')</script>";
    }
    }
    }
    ?> 

HTML代码

 <form name="register"  action="register.php" method="POST" id="register" style="font-family:ff-meta-web,Arial,sans-serif;text-align:justify;line-height:25px; font-size:12px;">
    <label style="margin-right:30px;"> <b> Name : </b></label>

    <input type="text" name="name" id="name" style="width:200px;" /> 
    <br /> <br />

    <label> <b> User Name : </b></label>
    <input type="text" name="username" id="username" style="width:200px;" /> <br/><br/>
    <label> <b> Password : </b></label> 
    <input type="password" name="password" id="password" style="width:200px;" />
    <br /> <br /> 
    <label> <b>Confirm Password : </b></label> 
    <input type="password" name="cpassword" id="cpassword" style="width:200px;" />
    <br /> <br /> 
    <input type="submit" name="submit" value="Submit" style="background-color:#005797; color:#fff; border:0px; padding:5px 10px;" /> 

    </form>
php validation passwords
2个回答
0
投票

更改

if($password=='' && $password<6){

if($password=='' && strlen($password)<6){

您还应在客户端验证所有输入。


0
投票

if($ password ==''&& strlen($ password)<6){echo“your error message”;} elseif($ password!= $ cpassword){echo“error message”;}你可以显示这样的错误信息

© www.soinside.com 2019 - 2024. All rights reserved.