jQuery 验证器 $.validator.methods[method] 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14896205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:03:33  来源:igfitidea点击:

validator $.validator.methods[method] is undefined

jqueryjquery-validate

提问by Mark

I have this validator form code I have inherited which I am getting this error:

我有我继承的这个验证器表单代码,我收到了这个错误:

$.validator.methods[method] is undefined 

and under that it's showing

在它下面它显示

'maxlength:$.validator.format("Please enter no more than {0} characters.")'

I have read that it could be spelling mistake or method being called that's not there. But can't see it.. even read about remote data element could be causing issue but taken it out it breaks.

我读过这可能是拼写错误或调用的方法不存在。但是看不到它......即使阅读远程数据元素也可能导致问题,但将其取出却会中断。

edit:I have uploaded none compressed js code and error points to this line

编辑:我上传了未压缩的 js 代码和错误点到这一行

var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );

code:

代码:

$(document).ready(function(){
    $("#regForm").validate({
        rules:{
            confirmemailaddress: {
                equalTo: "#emailaddress" 
            },
            password: {
                password: "#username"
            },              
            adminpassword: {
                adminpassword: "#adminusrname"
            },              
            retypepassword: {
                equalTo: "#password" 
            },
            retypenewpassword: {
                equalTo: "#newpassword" 
            },
            retypeadminpassword: {
                equalTo: "#adminpassword" 
            },
            interest2: {
                notEqualTo: "#interest"
            },
            retypenewadminpassword: {
                equalTo: "#newadminpassword" 
            },
            emailaddress: {             
                remote: {
                    url: $("#validationUrl").val(),
                    type: "post",
                    data: {
                          emailaddress: function() {
                            return $("#emailaddress").val();
                          },
                          registrationtype: function() {
                            return $("#registrationtype").val();
                          },
                          userctx: function() {
                             return $("#userctx").val();
                          },                                                                                          
                          identityid: function() {
                             return $("#identityid").val();
                          }                                                                                          
                     }
                }
            },
            username: {                
                remote: {
                    url: $("#validationUrl").val(),
                    type: "post",
                    data: {
                          username: function() {
                            return $("#username").val();
                          },                            
                          registrationtype: function() {
                             return $("#registrationtype").val();
                          },
                          userctx: function() {
                             return $("#userctx").val();
                          }                                                            
                     }
                }
            },
            adminusrname: { 
                notEqualTo: "#username",                 
                remote: {
                    url: $("#validationUrl").val(),
                    type: "post",
                    data: {
                          username: function() {
                            return $("#adminusrname").val();
                          },                            
                          registrationtype: function() {
                             return $("#registrationtype").val();
                          },
                          userctx: function() {
                           return $("#userctx").val();
                          }                                                            
                    }
                }                   
            },
            oldauthusername: {                
                remote: {
                    url: $("#validationUrl").val(),
                    type: "post",
                    data: {
                          username: function() {
                            return $("#oldauthusername").val();
                          },                            
                          registrationtype: function() {
                             return $("#registrationtype").val();
                          },
                          userctx: function() {
                             return $("#userctx").val();
                          }                                                            
                     }
                }
            }
        },
         messages: {
             instname:{          
                required: "Please specify the name of your institution"
              },
              firstname:{
                required: "Please specify your First Name",
                minlength: jQuery.format("At least {0} characters required!")
              },
             lastname:{
                required: "Please specify your Name",
                minlength: jQuery.format("At least {0} characters required!")
             },          
             interest:{
                required: "Please choose an interest area"
             },          
             interest2:{
                required: "Please choose a second interest area",
                notEqualTo: "Please choose a different interest area to the above"
             },          
             emailaddress:{
                 required: "We need your Email Address to contact you",
                 email: "Your Email Address must be in the format [email protected]",
                 remote: jQuery.format("{0} is already in use") 
            },
            confirmemailaddress:{
                 required: "Please confirm your Email Address",
                 email: "Your Email Address must be in the format [email protected]",
                 equalTo: "Please enter the same Email Address as above" 
            },
            username: {
                required: "Please specify your User Name",
                minlength: jQuery.format("At least {0} characters required!"),
                remote: jQuery.format("{0} is already in use") 
            },
            oldauthusername: {
                required: "Please specify your User Name",
                minlength: jQuery.format("At least {0} characters required!"),
                remote: jQuery.format("{0} is already in use") 
            },
            password: {
                required: "Please specify your Password",
                minlength: jQuery.format("At least {0} characters required!")
            },
            retypepassword: {
                required: "Please retype your Password",
                minlength: jQuery.format("At least {0} characters required!"),
                equalTo: "Enter the same password as above" 
            },
            adminusrname: {
                required: "Please specify your User Name",
                minlength: jQuery.format("At least {0} characters required!"),
                remote: jQuery.format("{0} is already in use"), 
                notEqualTo: "Your username must be different to the institution username" 
            },
            adminpassword: {
                required: "Please specify your Password",
                minlength: jQuery.format("At least {0} characters required!")
            },
            retypeadminpassword: {
                required: "Please retype your Password",
                minlength: jQuery.format("At least {0} characters required!"),
                equalTo: "Enter the same Password as above" 
            },              
            retypenewpassword: {
                required: "Please retype your New Password",
                minlength: jQuery.format("At least {0} characters required!"),
                equalTo: "Enter the same password as above" 
            },
            termsandconditions: {
                required: "Please tick the checkbox to confirm that you have read and agree to the terms and conditions before proceeding."
            }
        }
   });

    $("#ipmarker .asterix").hide();                      
    $("#logincredmarker .asterix").hide();     

    $("#validateContact").click(function() {
        var errors = false;
        var firstError;
        $(".contact .required").each(function() {
            if(!$("#regForm").validate().element(this))
            {
                if (! errors)
                    firstError = $(this);
                errors = true;          
            }
        });
        $(".contact .email").each(function() {
            if(!$("#regForm").validate().element(this))
            {
                if (! errors)
                    firstError = $(this);
                errors = true;          
            }
        });
        if(errors)
        {
            firstError.focus();
            return false;
        }
        else
        {
            $(".contact").hide();
            $(".authentication").show();
            if($('#usrname').is(':checked'))
                $("#usrname").parent("legend").parent("fieldset").find(".regInput").find(".optRequired").addClass("required");  
            $(".regStep2").removeClass("linkDisabled"); 
            scroll(0,0);
            return false;   
        }
    });

    var validateAuthenticationStep = function(){
                    if ( (   $("#oldauthhiddenusername").val()  != null && $("#oldauthhiddenusername").val() != '')
                      || (   $("#oldauthusername").val()        != null && $("#oldauthusername").val()       != '')
                      || (   $("#iprange").val()                != null && $("#iprange").val()               != '')
                      || (   $("#username").val()               != null && $("#username").val()              != '')
                      || (   $("#externalid").val()             != null && $("#externalid").val()            != '')
                       )
        {           
            var errors = false;
            $(".authentication .required, .authentication .checkIP, .authentication .email").each(function() {
                if(!$("#regForm").validate().element(this))
                {
                    if (! errors)
                        firstError = $(this);
                    errors = true;          
                }
            });
            if(errors)
            {
                firstError.focus();
                return false;
            }
            else
                return true;
        }
        else
        {
            $(".authenticationselectionerror").show();
            scroll(0,0);                
        }
    }

    $("#validateAuthentication").click(function() {
        if($("#username").val()!="") {
            $("#password").addClass("required");
            $("#retypepassword").addClass("required");
        }
        else
        {
            $("#password").removeClass("required");
            $("#retypepassword").removeClass("required");
        }

        if (validateAuthenticationStep())
        {
            $(".authentication").hide();
            $(".authenticationAdmin").show();
            $(".regStep3").removeClass("linkDisabled"); 
        }           
        return false;
    });

    $("#validateUpdateAuthentication").click(function() {
        if(validateAuthenticationStep())
            $("#regForm").submit();
        return false;
    });     





});

回答by Sparky

You are getting this error because you're calling a non-existent rule...

您收到此错误是因为您正在调用一个不存在的rule...

rules: {
    //other rules,
    interest2: {
        notEqualTo: "#interest"
    },
}

notEqualTois not a valid rule included within this plugin. You must remove it or create a new rule called notEqualToby using the plugin's built-in addMethod()method. Something like this...

notEqualTo不是此插件中包含的有效规则。您必须删除它或创建一个notEqualTo使用插件的内置addMethod()方法调用的新规则。像这样的东西...

jQuery.validator.addMethod('notEqualTo', function(value, element, param) {
    return value != jQuery(param).val();
}, 'Must not be equal to {0}.' );