jQuery 验证器 - 离开焦点后检查输入字段
jQuery validator - Check input field after leaving focus
我想检查输入字段 inlineViewProvider_ONLYOFFICE_URL
是否以 https://
开头 - 在离开字段焦点后。
到目前为止,这是我的代码:
$(document).ready(function() {
jQuery.validator.addMethod("httpsStarting", function (fieldInput, element) {
return this.optional(element) || fieldInput.match("^https:\/\/");
}, "Field input should start with 'https://'.");
$('#inlineViewProvider_ONLYOFFICE_URL').validate({
rules : { inlineViewProvider_ONLYOFFICE_URL: { httpsStarting: true } }
});
});
如果我将 console : console.log($('#inlineViewProvider_ONLYOFFICE_URL'))
放入 rules
值中,该字段值将打印到控制台。但如果输入字段不是以 https://
.
开头,则不会出现错误消息
我建议您尝试使用 test() instead of match() 的解决方案。
根据文档,
test() returns Boolean e.g. true
or false
.
然而,
match() returns an Array containing the matched results or null
if there were no matches.
所以null
在这里表示false
。
这可能是 IMO 的原因。
jQuery.validator.addMethod("httpsStarting", function (fieldInput, element) {
return this.optional(element) || /^https?:\/\//i.test(fieldInput);
}, "Field input should start with 'https://'.");
这是我目前可以通过查看代码段提出的建议。如果它不起作用,请告诉我。
只是一个疯狂的猜测:
关于.validate()
的jquery validation documentation谈到
Description: Validates the selected form.
您的 #inlineViewProvider_ONLYOFFICE_URL
是表单,还是 "just" 输入字段?
there is no error message if the input field doesn't start with https://
.
那是因为您显然已将 .validate()
方法而不是 form
方法附加到输入字段。换句话说,你还没有初始化插件。
$('#inlineViewProvider_ONLYOFFICE_URL').validate({ ....
要初始化插件以在您的 form
上工作,您可以将 .validate()
附加到代表 <form>
的 jQuery 选择器,其中包含 inlineViewProvider_ONLYOFFICE_URL
元素。
$('#yourForm').validate({
rules: {
inlineViewProvider_ONLYOFFICE_URL: { // <- this must represent the NAME attribute
httpsStarting: true
}
}
});
在 rules
对象中,inlineViewProvider_ONLYOFFICE_URL
必须是此输入元素的 name
属性的值。
<form id="yourForm">
<input type="text" name="inlineViewProvider_ONLYOFFICE_URL" />
....
</form>
有关正确使用的提示,请参阅 the Tag Wiki page。
我想检查输入字段 inlineViewProvider_ONLYOFFICE_URL
是否以 https://
开头 - 在离开字段焦点后。
到目前为止,这是我的代码:
$(document).ready(function() {
jQuery.validator.addMethod("httpsStarting", function (fieldInput, element) {
return this.optional(element) || fieldInput.match("^https:\/\/");
}, "Field input should start with 'https://'.");
$('#inlineViewProvider_ONLYOFFICE_URL').validate({
rules : { inlineViewProvider_ONLYOFFICE_URL: { httpsStarting: true } }
});
});
如果我将 console : console.log($('#inlineViewProvider_ONLYOFFICE_URL'))
放入 rules
值中,该字段值将打印到控制台。但如果输入字段不是以 https://
.
我建议您尝试使用 test() instead of match() 的解决方案。
根据文档,
test() returns Boolean e.g.
true
orfalse
.
然而,
match() returns an Array containing the matched results or
null
if there were no matches.
所以null
在这里表示false
。
这可能是 IMO 的原因。
jQuery.validator.addMethod("httpsStarting", function (fieldInput, element) {
return this.optional(element) || /^https?:\/\//i.test(fieldInput);
}, "Field input should start with 'https://'.");
这是我目前可以通过查看代码段提出的建议。如果它不起作用,请告诉我。
只是一个疯狂的猜测:
关于.validate()
的jquery validation documentation谈到
Description: Validates the selected form.
您的 #inlineViewProvider_ONLYOFFICE_URL
是表单,还是 "just" 输入字段?
there is no error message if the input field doesn't start with
https://
.
那是因为您显然已将 .validate()
方法而不是 form
方法附加到输入字段。换句话说,你还没有初始化插件。
$('#inlineViewProvider_ONLYOFFICE_URL').validate({ ....
要初始化插件以在您的 form
上工作,您可以将 .validate()
附加到代表 <form>
的 jQuery 选择器,其中包含 inlineViewProvider_ONLYOFFICE_URL
元素。
$('#yourForm').validate({
rules: {
inlineViewProvider_ONLYOFFICE_URL: { // <- this must represent the NAME attribute
httpsStarting: true
}
}
});
在 rules
对象中,inlineViewProvider_ONLYOFFICE_URL
必须是此输入元素的 name
属性的值。
<form id="yourForm">
<input type="text" name="inlineViewProvider_ONLYOFFICE_URL" />
....
</form>
有关正确使用的提示,请参阅 the Tag Wiki page。