1

更新


多亏了charlietfl 的评论和建议(以及,在某一时刻,我很生气,大声笑——为我的失礼道歉),我终于从 Validate 中检查了系统,并且在发送电子邮件时停止了表单提交。所以我想我的问题已经回答了,但是如果你们都再忍耐我一会儿,我可以用你们的帮助来做最后的画龙点睛……

在我最初的设想中,除了触发正确的“电子邮件已存在”错误之外,我还使用一些 HTML 填充了第二个元素,该 HTML 更完整地向用户解释了情况并提供了登录表单的链接。这第二个元素根据字段的状态出现和消失。

有没有办法使用消息/远程部分来做到这一点?

这是我所拥有的:

$(document).ready(function(){
$("#signup").validate({
    errorElement: "span",
    errorPlacement: function(error, element) {
        error.appendTo(element.prev());
        //element.prev().replaceWith(error);
    },
    rules: {
        "email": {
            required: true,
            email:true,
            remote: {
                url: "/ajax/emailcheck.php",
                type: "post",
            },
        },
        "password": {
            required: true,
            minlength: 8,
        },
        "password-check": {
            required: true,
            minlength: 8,
            passmatch: true,
        },
        "tos": {
            required: true,
            minlength: 6,
        }
    },
    messages: {
        email: {
            required: " is Required",
            email: " is Improperly Formatted",
            remote: " already exists",
            },

        },
        password: {
            required: " is Required",
            minlength: " requires at least 8 characters",
        },
        "password-check": {
            required: " is Required",
            minlength: " requires at least 8 characters",
            passmatch: " must match the Passphrase",
        },
        tos: {
            required: " is Required",
            minlength: " requires at least 6 characters",
        },                      
    },
    onkeyup: true,
    onblur: true
});

而且,在理想情况下,我会喜欢这样的东西:

messages: {
email: {
    required: " is Required",
    email: " is Improperly Formatted",
    remote: " already exists",
    remote: {
        username: function() {
        var emailcheck = $('#email').val();  
        return $('#username_availability_result').html(emailcheck + ' is already in our system. Please <a href="/login.php">log in here</a>.');
        },
    },
},              
},

再次感谢您一直以来的关注和建议,

Z

原始问题


我正在使用 jQuery Validate 在注册表单上运行常规验证。但我想添加到表单功能的功能之一是 AJAX 检查以确定电子邮件地址是否已在系统中。问题是电子邮件检查功能存在于验证功能之外,因此实际上并没有在必要时阻止表单提交。

这是我的代码。(前 50 行包括验证和密码匹配。其余的构成 AJAX 检查 [由电子邮件字段的 keyup 事件触发])。

        // Method adds password matching abilities to the validator
        jQuery.validator.addMethod("passmatch", function(value, element) {
           return $('#password').val() == $('#password-check').val()
        }, "* Passwords should match");

        $(document).ready(function(){
            $("#signup").validate({
                errorElement: "span",
                errorPlacement: function(error, element) {
                    error.appendTo(element.prev());
                    //element.prev().replaceWith(error);
                },
                rules: {
                    "email": {
                        required: true,
                        email:true,
                    },
                    "password": {
                        required: true,
                        minlength: 8,
                    },
                    "password-check": {
                        required: true,
                        minlength: 8,
                        passmatch: true,
                    },
                    "tos": {
                        required: true,
                        minlength: 6,
                    }
                },
                messages: {
                    email: {
                        required: " is Required",
                        email: " is Improperly Formatted",
                    },
                    password: {
                        required: " is Required",
                        minlength: " requires at least 8 characters",
                    },
                    "password-check": {
                        required: " is Required",
                        minlength: " requires at least 8 characters",
                        passmatch: " must match the Password"
                    },
                    tos: {
                        required: " is Required",
                        minlength: " requires at least 6 characters",
                    },                      
                }
            });

            //check email availability 
            $('#email').keyup(function(){  
                check_availability();  
            });  

        });               

        //function to check username availability  
        function check_availability(){  

                //get the username  
                var username = $('#email').val();  

                //use ajax to run the check  
                $.post("/ajax/emailcheck.php", { username: username },  
                    function(result){  
                        //if the result greater than none  
                        if(result > 0 ){  
                            //show that the username is not available  
                            $('#username_availability_result').html(username + ' is already in our system. Please <a href="/login.php">log in here</a>.');  
                        }else{  
                            //username available.  
                            //clear any messages
                            $('#username_availability_result').html(''); 
                        }  
                });  
        }  

有没有办法让 check_availability() 函数触发停止(以及在清除后启动),以便在错误状态下无法提交表单?或者整个套件和 caboodle 是否可以使用 addMethod 以某种方式集成到 Validate 中(如果是这样,请注意我在特定 ID 的元素中提供可用性反馈,而不是通过出现其他 Validate 错误的同一元素)?

提前感谢您的所有帮助和建议。

Z

4

2 回答 2

3

使用已经具有内置 ajax 方法的验证插件的远程选项,该方法将绑定到输入

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

或者required也可以是一个函数(不会在 keyup 或 blur 上工作)

email:{ required: function(){
          return $('#username_availability_result').html()=='';
   }

}

另外,如果 ajax 返回重复,为什么不重置电子邮件字段?您的代码可能会在重置字段时按原样工作

最好的建议是使用内置remote

于 2012-06-24T23:08:46.273 回答
0
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("#singupfrom").validate({
                rules: {

                    'useremail': {// compound rule
                        required: true,
                        email: true,



                        remote:{


                            url: "check_email.php",
                            type: "post",

                            data:
                            {
                                emails: function()
                                {
                                    return $('#singupfrom :input[name="useremail"]').val();
                                }
                            }


                        }
                    }

                },
                  // here custom message for email already exists
                 messages: {
                    useremail: { remote: "Email already exists"}

                }

            });

        });
    </script>

    <!-- your user email-->

                                <label>Email :</label>
                                <input type="text" name="useremail" id="useremail" value=""  />
    <!-- your user email end -->



    // your  php file "check_email.php"  will be some thing like it 
    /// get or post can also be used in place of request depending on situation
    $email = $_REQUEST['useremail'];


    <?php 
            $check   = "your query to check the email and returns the no of rows/ emails exists ";
            if ($check == '0' or empty($check)) {
                echo 'true';
            } else {
                echo  'false';

            }

?>
于 2012-11-26T07:56:37.580 回答