jQuery验证无法验证我的信息

时间:2013-12-09 04:44:46

标签: javascript php jquery jquery-validate

我需要在注册期间对我的用户名进行验证,遗憾的是我无法从屏幕上获取任何信息,当我点击提交按钮时没有响应。

已更新

这是我正在执行的登录和注册脚本,但是我无法显示错误消息,并且在执行检查时也显示不正确的css样式。

http://img809.imageshack.us/img809/9749/af86.png

以下是截屏视频http://www.screencast.com/t/hQdRev1HOnh

        <script>
        $(document).ready(function() {

            //* boxes animation
            form_wrapper = $('.login_box');
            function boxHeight() {
                form_wrapper.animate({marginTop: (-(form_wrapper.height() / 2) - 24)}, 400);
            }
            ;
            form_wrapper.css({marginTop: (-(form_wrapper.height() / 2) - 24)});
            $('.linkform a,.link_reg a').on('click', function(e) {
                var target = $(this).attr('href'),
                        target_height = $(target).actual('height');
                $(form_wrapper).css({
                    'height': form_wrapper.height()
                });
                $(form_wrapper.find('form:visible')).fadeOut(400, function() {
                    form_wrapper.stop().animate({
                        height: target_height,
                        marginTop: (-(target_height / 2) - 24)
                    }, 500, function() {
                        $(target).fadeIn(400);
                        $('.links_btm .linkform').toggle();
                        $(form_wrapper).css({
                            'height': ''
                        });
                    });
                });
                e.preventDefault();
            });
            //* validation
            $('#login_form').validate({
                onkeyup: false,
                errorClass: 'error',
                validClass: 'valid',
                rules: {
                    username: {required: true, minlength: 3},
                    password: {required: true, minlength: 3}
                },
                highlight: function(element) {
                    $(element).closest('div').addClass("f_error");
                    setTimeout(function() {
                        boxHeight()
                    }, 200)
                },
                unhighlight: function(element) {
                    $(element).closest('div').removeClass("f_error");
                    setTimeout(function() {
                        boxHeight()
                    }, 200)
                },
                errorPlacement: function(error, element) {
                    $(element).closest('div').append(error);
                }
            });
            $('#reg_form').validate({
                onkeyup: false,
                errorClass: 'error',
                validClass: 'valid',
                rules: {
                    reg_username: {
                        required: true,
                        minlength: 3,
                        remote: {
                            url: "http://127.0.0.1/check_username.php",
                            type: "post"
                        }
                    },
                    message: {
                        reg_username: {remote: jQuery.format("{0} is already in use")}
                    },
                    reg_password: {required: true, minlength: 3}
                },
                highlight: function(element) {
                    $(element).closest('div').addClass("f_error");
                    setTimeout(function() {
                        boxHeight()
                    }, 200)
                },
                unhighlight: function(element) {
                    $(element).closest('div').removeClass("f_error");
                    setTimeout(function() {
                        boxHeight()
                    }, 200)
                },
                errorPlacement: function(error, element) {
                    $(element).closest('div').append(error);
                }
            });
        });
    </script>

这是我的用户名检查源代码。虽然我更改了字符串结果的返回值,但我无法显示错误消息。

    include('../inc/dbconn.php');

if (isset($_POST['reg_username'])) {
    $reg_username = mysql_real_escape_string($_POST['reg_username']);
    $check_for_username = mysql_query("SELECT username FROM customers_register WHERE username='$reg_username'");
    if (mysql_num_rows($check_for_username)) {
        echo 'false';
    } else {
        //No Record Found - Username is available
        echo 'true';
    }
}

这是我的一部分javascript

 $('#reg_form').validate({
        onkeyup: false,
        errorClass: 'error',
        validClass: 'valid',
        rules: {
            reg_username: {required: true, minlength: 3, remote:"check_username.php"},
            reg_password: {required: true, minlength: 3}
        },
        message: {  
            reg_username: {remote: jQuery.format("{0} is already in use")}
        },
        highlight: function(element) {
            $(element).closest('div').addClass("f_error");
            setTimeout(function() {
                boxHeight()
            }, 200)
        },
        unhighlight: function(element) {
            $(element).closest('div').removeClass("f_error");
            setTimeout(function() {
                boxHeight()
            }, 200)
        },
        errorPlacement: function(error, element) {
            $(element).closest('div').append(error);
        }
    });

这是我的check_username.php

<?php

include('../inc/dbconn.php');

if (isset($_POST['reg_username'])) {
    $reg_username = mysql_real_escape_string($_POST['reg_username']);
    $check_for_username = mysql_query("SELECT username FROM customers_register_user WHERE username='$reg_username'");
    if (mysql_num_rows($check_for_username)) {
        echo "TRUE";
    } else {
        //No Record Found - Username is available
        echo "FALSE";
    }
}
?>

这是html源代码

            <form method="post" id="reg_form" style="display:none">

            <div class="top_b">Sign up</div>
            <div class="alert alert-login">
                By filling in the form bellow and clicking the "Sign Up" button, you accept and agree to <a data-toggle="modal" href="#terms">Terms of Service</a>.
            </div>
            <div id="terms" class="modal hide fade" style="display:none">
                <div class="modal-header">
                    <a class="close" data-dismiss="modal">×</a>
                    <h3>Terms and Conditions</h3>
                </div>                    
                <div class="modal-footer">
                    <a data-dismiss="modal" class="btn" href="#">Close</a>
                </div>
            </div>
            <div class="cnt_b">      
                <? echo '<input type = "hidden" name = "client_mac" value = "' . $client_mac . '">'; ?>                      
                <div class="formRow">
                    <div class="input-prepend">
                        <span class="add-on"><i class="icon-user"></i></span>
                        <input type="text" id="reg_username" name="reg_username" placeholder="Username" />
                    </div>
                </div>
                <div class="formRow">
                    <div class="input-prepend">
                        <span class="add-on"><i class="icon-lock"></i></span><input type="password" id="reg_password" name="reg_password" placeholder="Password" />
                    </div>
                </div>
                <div class="formRow">
                    <div class="input-prepend">                           
                        <span class="add-on">@</span><input type="text" disabled="disabled" id="email" placeholder="<?= $email ?>" />

                    </div>
                    <small>The e-mail address is not made public and will only be used if you wish to receive a new password.</small>
                </div>

            </div>
            <div class="btm_b tac">
                <button class="btn btn-inverse" name="oprf" value="signup" type="submit">Sign Up</button>
            </div>
        </form>

2 个答案:

答案 0 :(得分:0)

看来你的jquery代码很好。你可以这样调试你的脚本: -

    <?php
    error_reporting(E_ALL);
    include('../inc/dbconn.php');

    if (isset($_POST['reg_username'])) {
        $reg_username = mysql_real_escape_string($_POST['reg_username']);
        $check_for_username = mysql_query("SELECT username FROM customers_register_user WHERE username='$reg_username'") or die(mysql_error());
        // If 0 reocrd found then username available
        if (mysql_num_rows($check_for_username) > 0) {
            echo 0;
        } else {
            //No Record Found - Username is available
            echo 1;
        }
    }
    ?>

答案 1 :(得分:0)

引用OP:

  

“这是最新的更新似乎我可以从验证中获得TRUEFALSE,但问题是虽然我已输入值但它仍会弹出错误,我无法提交注册。“

您的代码......

if (mysql_num_rows($check_for_username)) {
    echo "TRUE";
} else {
    //No Record Found - Username is available
    echo "FALSE";
}

您的true/false逻辑完全倒退。在jQuery Validate插件中,如果方法返回true,则表示元素通过验证,false表示元素验证失败(触发错误消息)。

当用户名已经在您的数据库中时,您正在回显"TRUE",但是,您应该回显"FALSE"以指示此字段验证失败。


修改

根据文件:

  

响应评估为JSON,必须为true有效元素,   对于无效元素,可以是falseundefinednull ...

http://jqueryvalidation.org/remote-method/