问题
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
termis a rule, you can't nest a rule (regex) inside of a rule (term). termis not a "built-in"ruleas per the documentation.termis also not a "custom" rule as per your code.- If
termis a fieldname, 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