JQuery validation regex for spaces receiving "Uncaught TypeError: cannot call invocation method undefined error

Here is my jquery code as the previous question https://stackoverflow.com/a/464632/

$('#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'm trying to do is show a popover if there is a space in the entered term. But every time he gives me an error

 Uncaught TypeError: Cannot call method 'call' of undefined 

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

PS I am using twitter bootstrap for popover.

UPDATE: More on 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 
-one
jquery jquery-validate twitter-bootstrap
Apr 18 '13 at 15:40
source share
2 answers

I assume that you should use this.errorList instead of errorList in the second $.each . It is also possible that the difference between the two loops in value.id and value.element.id also significant.

+1
Apr 18 '13 at 15:44
source share

The rules declaration structure 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 should be the term ? And why regex inside term ?

  • If term is a rule, you cannot insert a rule ( regex ) inside a rule ( term ).
  • term not a "built-in" rule according to the documentation .
  • term also not a β€œnormal” rule according to your code.
  • If term is a name field, you cannot insert a field ( term ) inside a field ( product ).

This explains your mistake,

"Uncaught TypeError: Unable to call the 'call' method from 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 , this field checks if it returns false , the field will be invalid and an error will be displayed.

+1
Apr 20 '13 at 15:32
source share



All Articles