简体   繁体   中英

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. 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
  2. in IE8: 'validator' is null or not an object
  3. FireFox: no error message, but the JQuery default validation logic breaks;

The piece of code which is causing the error to occur is this (it sits inside the ready function):

$().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.

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.

Like this:

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

or whatever you use to access Jquery object

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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