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

时间:2015-03-16 06:43:25

标签: php validation passwords

我正在尝试一个注册表单,其中密码和确认密码应该相互匹配。如果不匹配则应显示错误消息。以及如何验证密码超过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 CODE

 <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>

2 个答案:

答案 0 :(得分:0)

更改

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

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

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

答案 1 :(得分:0)

if($ password ==&#39;&#39;&amp;&amp; strlen($ password)&lt; 6){echo&#34; your error mesaage&#34;;} elseif($ password!= $ cpassword){echo&#34; error meassage&#34 ;;} 你可以显示这样的错误信息