在表单中显示密码不匹配或其强度的错误消息

时间:2013-10-16 10:51:45

标签: javascript php jquery html

我有以下代码:

<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?另请告诉我打印表单中的所有消息的方法。提前谢谢。

4 个答案:

答案 0 :(得分: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.";
}
?>

答案 1 :(得分:0)

首先,请确保<form>操作是""或自己的页面名称,因为它看起来就像您想要的那样。

然后,你应该做

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.";
    }
}

答案 2 :(得分:0)

使用jquery进行客户端验证有很多可用的jquery插件 给出了一些链接,您可以查看演示,

http://jqueryvalidation.org/ http://www.tutorialspoint.com/javascript/javascript_form_validations.htm

http://rickharrison.github.io/validate.js/ 我的个人建议是使用验证js。 alos也实现了服务器端验证,因为可以在浏览器中禁用javascript。

使用php进行服务器端验证,请参阅此链接

http://phpformvalidation.monjur-ul-hasan.info/

希望它对你有所帮助。

答案 3 :(得分: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!');
        }
    });
});