2013-10-16 235 views
0

我有以下代码:显示错误消息

<fieldset> 
<p>      
    <label>Password*</label> 
    <input type="password" placeholder="Password" name="pswd" id="psd"/> 

    <label>Confirm Password*</label> 
    <input type="password" name="cpswd" placeholder=" retype Password" id="cpsd" /> 

    <label class="obinfo">* obligatory fields</label> 
</p> 
</fieldset> 

<?php 
    $pwd = $_POST['pswd']; 

    if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd)) 
    { 
     echo "Your password is good."; 
    } 
    else 
    { 
     echo "Your password is bad."; 
    } 
?> 

目前的代码只是检查,如果密码是好(强)还是不。它给错误未定义索引pswd。我在这方面是一个新手。我不太了解JavaScript或jQuery。有没有一种方法可以检查密码强度,并查看它们是否使用php进行匹配?也让我知道方法来打印表单中的所有消息。提前致谢。

回答

1

用途:所有的

<?php 
if(isset($_POST['pswd'])){//You need to check if $_POST['pswd'] is set or not as when user visits the page $_POST['pswd'] is not set. 
$pwd = $_POST['pswd']; 

if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd)) 
    echo "Your password is good."; 
else 
    echo "Your password is bad."; 
} 
?> 
+2

尝试Subhankers和@BeatAlex的答案,但把你的PHP上面的格式HTML,所以你检查,看看它是否是一个良好的密码之前,表单呈现。这样你就可以把表单中的消息从php中输出。 – Gavin

0

首先,请确保<form>动作或者是""或自己的网页名称,因为它看起来像这就是你想要它去到什么。

然后,你应该使用jQuery的客户端验证做

if (isset($_POST['pswd'])) 
{ 
    $pwd = $_POST['pswd']; 

    if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd)){ 
     echo "Your password is good."; 
    } else { 
     echo "Your password is bad."; 
    } 
} 
0

说真的,你可以使用某种这样的:http://jsfiddle.net/3QhBu/,但有一个更好的方法 - 自己动手。

$(function(){ 
    var $spanMsg = $('<span/>', {class: 'span-msg'}); 

    $('input#psd').on('keyup.checkPswd', function(){ 
     var $that = $(this); 
     var $thisSpanMsg = $that.siblings('span.span-msg').length ? $that.siblings('span.span-msg') : $spanMsg.clone().insertAfter($that); 
     if (!/.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$/.test($that.val())) { 
      $thisSpanMsg.removeClass('span-msg-green').addClass('span-msg-red').text('Wrong password!');  
     } else { 
      $thisSpanMsg.removeClass('span-msg-red').addClass('span-msg-green').text('Password is correct!'); 
     } 
    }); 

    $('input#cpsd').on('keyup.checkPswdMatch', function(){ 
     var $that = $(this); 
     var $thisSpanMsg = $that.siblings('span.span-msg').length ? $that.siblings('span.span-msg') : $spanMsg.clone().insertAfter($that); 
     if ($that.val() != $('input#psd').val()) { 
      $thisSpanMsg.removeClass('span-msg-green').addClass('span-msg-red').text('Passwords don\'t match!');  
     } else { 
      $thisSpanMsg.removeClass('span-msg-red').addClass('span-msg-green').text('Confirm password is correct!'); 
     } 
    }); 
});