空格的jQuery验证正则表达式获取“未捕获的TypeError:无法调用方法'call'的未定义错误" [英] jQuery validation regex for white space getting a "Uncaught TypeError: Cannot call method 'call' of undefined error"
问题描述
这是我以前的 stackoverflow问题
$('#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
我知道正则表达式部分出了问题.因为我用minLength尝试了相同的代码,所以效果很好.我在做什么错了?
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我正在使用Twitter引导程序弹出窗口.
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
推荐答案
rules
声明的结构如下...
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
}
},
现在输入您的代码:
rules: {
product: {
required: true,
term: {regex: /^\s*$/}
}
},
term
应该是什么?为什么regex
位于term
内?
What exactly is term
supposed to be? And why is regex
inside of term
?
- 如果
term
是规则,则不能将规则(regex
)嵌套在规则(term
)内. -
term
不是内置"rule
根据文档.
根据您的代码, -
term
也不是自定义"规则. - 如果
term
是字段name
,则不能将字段(term
)嵌套在字段(product
)内.
- 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 fieldname
, you can't nest a field (term
) inside of a field (product
).
这说明了您的错误,
未捕获的TypeError:无法调用未定义的方法'call'"
"Uncaught TypeError: Cannot call method 'call' of undefined"
换句话说,插件将term
视为未定义的方法.
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*$/
}
},
另外,请记住,如果您的自定义方法返回true
,则该字段将验证,如果返回false
,则该字段将通过验证失败,并显示错误.
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.
这篇关于空格的jQuery验证正则表达式获取“未捕获的TypeError:无法调用方法'call'的未定义错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!