2013-10-16 263 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!'); 
     } 
    }); 
});