0

I have following code:

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

Presently code just checks if password is good(strong) or not . It is giving error Undefined index pswd. I am a newbie in this regard. I do not know much about javascript or jQuery. Is there a way we can check the password strength and see if they are matching using php? Also do let me know way to print all the messages in the form itself. Thanks in advance.

4

4 回答 4

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.";
}
?>
于 2013-10-16T10:54:22.830 回答
0

对于使用 jquery 的客户端验证,有很多可用的 jquery 插件提供一些链接,您可以查看演示,

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

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

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

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

希望它可以帮助你。

于 2013-10-16T11:02:30.570 回答
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.";
    }
}
于 2013-10-16T10:56:28.780 回答
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!');
        }
    });
});
于 2013-10-16T11:29:25.757 回答