未捕获的TypeError:undefined不是函数

时间:2014-05-21 06:50:07

标签: javascript jquery forms validation

我在这一行中收到此错误:$(" #register-form")。validate。有人能帮我吗? 另外,如果我写这个脚本,我应该如何在表单中调用它?或者它会在不调用的情况下工作吗?

<script type="text/javascript">
(function($, W, D) {
           var JQUERY4U = {};
            JQUERY4U.UTIL = {
                    setupFormValidation : function() {
            //form validation rules
                $("#register-form").validate({
                rules : {
                    name : "required",
                    email : "required",
                    phone : "required"

                },
                messages : {
                    name : "Please enter your name",
                    email : "Please enter your email",
                    phone : "Please enter your phone"

                },
                submitHandler : function(form) {
                    if ($("#id").val() == "")
                        createRegistration();
                    else
                        alert("Error loading the data");
                }
            });
        }
    };

    //when the dom has loaded setup form validation rules
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);</script>

1 个答案:

答案 0 :(得分:0)

让我们改进您的代码并使用 a jsFiddle 为您提供更多帮助:

$(function(){
    jquery4u.util.setupFormValidation();
});

var jquery4u = {
    util: {
        setupFormValidation: function(){
            $("#register-form").validate({
                rules : {
                    name : "required",
                    email : "required",
                    phone : "required"
                },
                messages : {
                    name : "Please enter your name",
                    email : "Please enter your email",
                    phone : "Please enter your phone"
                },
                submitHandler : function(form) {
                    if ($("#id").val() == "")
                        createRegistration();
                    else
                        alert("Error loading the data");
                }
            });
        }
    }
}