简体   繁体   English

jQuery自定义验证错误

[英]JQuery custom validation error

I am facing the following issue with the JQuery validator: I have a form where I am using the default validation rules provided and everything works fine. 我在使用JQuery验证程序时遇到以下问题:我有一个使用我提供的默认验证规则的表单,并且一切正常。 Howoever, as soon as I add a custom validation, the following error is thrown: 但是,一旦添加自定义验证,就会引发以下错误:

  1. in Chrome: Uncaught TypeError: Cannot call method 'addMethod' of undefined 在Chrome中:未捕获的TypeError:无法调用未定义的方法'addMethod'
  2. in IE8: 'validator' is null or not an object 在IE8中:“验证程序”为null或不是对象
  3. FireFox: no error message, but the JQuery default validation logic breaks; FireFox:没有错误消息,但是JQuery默认验证逻辑中断;

The piece of code which is causing the error to occur is this (it sits inside the ready function): 导致错误发生的代码片段是这样的(它位于ready函数中):

$().validator.addMethod("defaultInvalid", 
   function(value, element) {   
    alert("validate");
   }
 );

As you see I have commented most of the code of the callback function in order to eliminate any other error. 如您所见,我已注释了大多数回调函数代码,以消除任何其他错误。 if I comment it, everything goes back to normal. 如果我发表评论,一切都会恢复正常。 However, I would like to use this customized rule on a text box taht must have the default value of "First name" and on submit it should not allow the user to send "First name" as value. 但是,我想在文本框上使用此自定义规则,因为此框必须具有默认值“ First name”,并且在提交时不应允许用户发送“ First name”作为值。

Looking at this, validator won't be found as it's looking inside $() . 看着这个,在$()里面找不到验证器。 You need to just use $.validator.addMeth.... which goes into the default jquery object. 您只需要使用$.validator.addMeth.... ,它将进入默认的jquery对象。

Like this: 像这样:

$.validator.addMethod("defaultInvalid", 
   function(value, element) {   
    alert("validate");
});

or whatever you use to access Jquery object 用于访问Jquery对象的任何内容

JQuery.validator.addMethod("defaultInvalid", 
   function(value, element) {   
    alert("validate");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM