没有调用jQuery验证

时间:2012-07-29 17:14:58

标签: javascript jquery

我正在构建PHP身份验证系统的前端我正在构建,我使用jQuery,我或多或少了解基础知识并且在登录,注册和激活页面显示错误/成功消息,在发送之前散列密码,为用户分配cookie并进行表单验证。

我目前正在使用重置系统,这部分是第2步,在用户收到重置请求电子邮件后,他们进入网站并输入重置代码,或者使用直接链接,然后点击按钮。然后jQuery向服务器发送一个验证重置密钥的请求,如果不正确,则显示相应的消息,如果正确,则清除.form div并将其替换为另一种形式(#resetpass)。在这种形式下,用户输入新密码,然后将其发送到服务器,但由于某种原因,jQuery没有抓住提交,表单正常提交,而不是通过Ajax,我已经现在看一下代码已经有一段时间了,改变这个和那个,没有积极的结果。我做错了什么?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHPAuth 2.0 - Reset Password</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<!-- Base CSS -->
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!-- Jquery CSS (UI + Addons) -->
<link href="css/ui-lightness/jquery-ui.custom.css" rel="stylesheet" type="text/css" />
<link href="css/jquery.qtip.css" rel="stylesheet" type="text/css" />
<!-- Jquery base file + Jquery UI -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.message.js"></script>
<!-- Jquery plugins for this page : To be compiled -->
<script type="text/javascript" src="js/reset/jquery.validate.js"></script>
<script type="text/javascript" src="js/reset/jquery.qtip.js"></script>
<script type="text/javascript" src="js/reset/jquery.crypt.js"></script>
</head>

<body>
<div class="logo"></div>
<div class="form">
    <form id="reset" method="post">
        <input type="text" name="resetkey" id="password" placeholder="Reset Key" <?php if(isset($_GET['key'])) { echo "value=\"" . $_GET['key'] . "\" "; } ?>/>
        <input type="submit" id="reset" value="Reset Password" />
    </form>
</div>
<div class="small">
    <a href="?page=login">I know my password</a><br/>
</div>

<script type="text/javascript">
$(document).ready(function(){
    $('#reset').validate({
            errorClass: "errormessage",
            onkeyup: false,
            errorClass: 'error',
            validClass: 'valid',
            rules: {
                password: { required: true, minlength: 3, maxlength: 100 }
            },
            errorPlacement: function(error, element)
            {
                var elem = $(element),
                    corners = ['right center', 'left center'],
                    flipIt = elem.parents('span.right').length > 0;

                if(!error.is(':empty')) {
                    elem.filter(':not(.valid)').qtip({
                        overwrite: false,
                        content: error,
                        position: {
                            my: corners[ flipIt ? 0 : 1 ],
                            at: corners[ flipIt ? 1 : 0 ],
                            viewport: $(window)
                        },
                        show: {
                            event: false,
                            ready: true
                        },
                        hide: false,
                        style: {
                            classes: 'ui-tooltip-red'
                        }
                    })
                    .qtip('option', 'content.text', error);
                }
                else { elem.qtip('destroy'); }
            },
            success: $.noop,
    })


    $('#resetpass').validate({
            errorClass: "errormessage",
            onkeyup: false,
            errorClass: 'error',
            validClass: 'valid',
            rules: {
                resetkey: { required: true, minlength: 20, maxlength: 20 }
            },
            errorPlacement: function(error, element)
            {
                var elem = $(element),
                    corners = ['right center', 'left center'],
                    flipIt = elem.parents('span.right').length > 0;

                if(!error.is(':empty')) {
                    elem.filter(':not(.valid)').qtip({
                        overwrite: false,
                        content: error,
                        position: {
                            my: corners[ flipIt ? 0 : 1 ],
                            at: corners[ flipIt ? 1 : 0 ],
                            viewport: $(window)
                        },
                        show: {
                            event: false,
                            ready: true
                        },
                        hide: false,
                        style: {
                            classes: 'ui-tooltip-red'
                        }
                    })
                    .qtip('option', 'content.text', error);
                }
                else { elem.qtip('destroy'); }
            },
            success: $.noop,
    })
});

$("#reset").submit(function(event) {
    if($("#reset").valid()) {
        event.preventDefault(); 

        var $form = $( this ),
            resetkey = $form.find('input[name="resetkey"]').val();


        $.post("inc/action.php?a=reset2", {key: resetkey},
            function(data) {
                if(data['error'] == 1)
                {
                    $("#message").remove();

                    $(".form").prepend('<div id="message"></div>');

                    $("#message").message({type: "error", dismiss: false, message: data['message']});

                    $("#message").effect("shake", {times: 2, distance: 10}, 200);
                }
                else if(data['error'] == 0)
                {                           
                    $(".form").empty();

                    $(".form").append('<form id="resetpass" method="post">');
                    $("#resetpass").append('<input type="hidden" name="resetkey" value="' + resetkey + '" />');
                    $("#resetpass").append('<input type="password" name="password" id="password" placeholder="New Password" />');
                    $("#resetpass").append('<input type="submit" id="reset" value="Change Password" />');
                }
            }, "json"
        );
    }
    else
    {
        $("[id^=ui-tooltip-]").effect("pulsate", {times: 3}, 300);
        return false;
    }
});

$("#resetpass").submit(function(event) {
    if($("#resetpass").valid()) {
        event.preventDefault(); 

        var $form = $( this ),
            resetkey = $form.find( 'input[name="resetkey"]' ).val(),
            pass = $().crypt({method:"sha1",source:$().crypt({method:"sha1",source:$form.find( 'input[name="password"]' ).val()})});

        $.post("inc/action.php?a=reset3", {key: resetkey, password: pass},
            function(data) {
                if(data['error'] == 1)
                {
                    $("#message").remove();

                    $(".form").prepend('<div id="message"></div>');

                    $("#message").message({type: "error", dismiss: false, message: data['message']});

                    $("#message").effect("shake", {times: 2, distance: 10}, 200);
                }
                else if(data['error'] == 0)
                {           
                    $("#message").remove();

                    $(".form").prepend('<div id="message"></div>');

                    $("#message").message({type: "info", dismiss: false, message: data['message']});

                    $("#message").effect("pulsate", {times: 2}, 200);
                }
            }, "json"
        );
    }
    else
    {
        $("[id^=ui-tooltip-]").effect("pulsate", {times: 3}, 300);
        return false;
    }
});
</script>
</body>
</html>

非常感谢任何帮助:)

2 个答案:

答案 0 :(得分:0)

为什么你有2个标签具有相同的ID'重置' 尝试而不是reset.submit

<form id="resetfrm" method="post" onsubmit="fnSubmit();return false;">

function fnSubmit(){
    if($("#reset").valid()) {      
        var $form = $( this ),
            resetkey = $form.find('input[name="resetkey"]').val();


        $.post("inc/action.php?a=reset2", {key: resetkey},
            function(data) {
                if(data['error'] == 1)
                {
                    $("#message").remove();

                    $(".form").prepend('<div id="message"></div>');

                    $("#message").message({type: "error", dismiss: false, message: data['message']});

                    $("#message").effect("shake", {times: 2, distance: 10}, 200);
                }
                else if(data['error'] == 0)
                {                           
                    $(".form").empty();

                    $(".form").append('<form id="resetpass" method="post">');
                    $("#resetpass").append('<input type="hidden" name="resetkey" value="' + resetkey + '" />');
                    $("#resetpass").append('<input type="password" name="password" id="password" placeholder="New Password" />');
                    $("#resetpass").append('<input type="submit" id="reset" value="Change Password" />');
                }
            }, "json"
        );
    }
    else
    {
        $("[id^=ui-tooltip-]").effect("pulsate", {times: 3}, 300);
        return false;
    }

}

答案 1 :(得分:0)

由于Shoky在irc.freenode.net上的#jquery中解决了问题

我必须抛出所有代码进行验证和提交,之后我在window_timeout函数中将内容添加到DOM中。