jQuery validation regex for white space getting a “Uncaught TypeError: Cannot call method 'call' of undefined error”

梦想的初衷 提交于 2019-12-13 08:39:42

问题


Here is my jquery code as per a previous stackoverflow question

$('#productId').validate({
     rules: {
         product: {
             required: true,
             term: {regex: /^$|\s/}
         }
     },
     messages: {
         product: {
             required: "A text is much",
             term: "Please avoid spaces"
         },
     },  

     showErrors: function (errorMap, errorList) {

         $.each(this.successList, function (index, value) {
             $('#'+value.id+'').popover('destroy');
         });


         $.each(errorList, function (index, value) {

             $('#'+value.element.id+'').attr('data-content',value.message, 'title', 'Oops!').popover({
                 placement: 'top',
                 trigger: 'manual',
                 delay: { show: 500, hide: 5000 }
             }).popover('show');

         });

     }

 });

What I am trying to do is show a popover if there is white space in the term entered. But every time it gives me the error

Uncaught TypeError: Cannot call method 'call' of undefined 

I know something is wrong with the regex part. Because the I tried the same code with minLength and it worked well. What am i doing wrong?

P.S I am using twitter bootstrap for popover.

UPDATE: More about the error

Uncaught TypeError: Cannot call method 'call' of undefined ----------jquery.validate.js:504

    $.extend.check ---------- jquery.validate.js:504

    $.extend.element ---------- jquery.validate.js:357
    $.extend.defaults.onfocusout ---------- jquery.validate.js:231

    delegate ---------- jquery.validate.js:317

    (anonymous function) ---------- jquery.validate.js:1184

    jQuery.event.dispatch ---------- jquery.js:3075

    elemData.handle ---------- jquery.js:2751

    jQuery.event.trigger ---------- jquery.js:2987
    jQuery.event.simulate ---------- jquery.js:3302

    handler

回答1:


My guess would be that you should be using this.errorList instead of just errorList in the second $.each. It's also possible that the difference between the two loops in value.id and value.element.id is significant too.




回答2:


The structure of the rules declaration is as follows...

rules: {                       // <- rules:
    field_name: {              // <- name attribute of field                   
        rule_name: parameter,  // <- rule: parameter
        required: true,   // example 1
        min: 30           // example 2
    }
},

Now your code:

rules: {
    product: {
        required: true,
        term: {regex: /^\s*$/}
    }
},

What exactly is term supposed to be? And why is regex inside of term?

  • If term is a rule, you can't nest a rule (regex) inside of a rule (term).
  • term is not a "built-in" rule as per the documentation.
  • term is also not a "custom" rule as per your code.
  • If term is a field name, you can't nest a field (term) inside of a field (product).

This explains your error,

"Uncaught TypeError: Cannot call method 'call' of undefined"

In other words, the plugin sees term as an undefined method.

Assuming your regex is correct, it should be like this...

rules: {
    product: {
        required: true,
        regex: /^\s*$/
    }
},

Also, remember that if your custom method returns true, the field validates, if it returns false, the field will fail validation and the error will display.



来源:https://stackoverflow.com/questions/16087351/jquery-validation-regex-for-white-space-getting-a-uncaught-typeerror-cannot-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!