jQuery Validator插件-无验证
我知道这很简单(无论如何,根据插件页面)
I know this suppose to be simple (according to the plugin page anyway)
但是我试图使用该插件没有成功,这是我的表格:
But I'm trying to use the plugin with no success, here's my form:
<form id="mongoForm">
<label for="skip">skip:</label> <input type="text" id="skip"
style="width: 70px">
<p />
limit: <span id="limitLable" style="border: 0">1</span>
<div id="limit" style="width: 250px"></div>
<p />
<label for="collection">collection: </label> <input type="text"
id="collection">
<p />
<label for="sort">sort: </label> <input type="text" id="sort">
<p />
<label for="type">type: </label><select id="type">
<option value="find" selected="selected">find</option>
<option value="count">count</option>
</select>
<p />
<label for="query">query: </label>
<textarea rows="10" cols="80" id="query"></textarea>
<input type="submit" value="execute" id="execute">
</form>
和我的jquery脚本:
and my jquery script:
$(document).ready(function() {
$.validator.addMethod("validJson", function(value, element) {
try {
var val = $.parseJSON(value);
return true;
} catch (e) {
return false;
}
}, "Invalid JSON string");
$("#mongoForm").validate({
rules : {
collection : {
required : true
},
query : {
validJson : true
}
},
submitHandler : function(form) {
queryServer();
}
});
但是,验证无法正常进行. 如果我在输入中添加class ="required",则它适用于自定义验证,但不适用于我的自定义validJson方法.但这不能解决我的问题,因为某些字段不是必需的,我只需要验证可选值即可.
But, the validation is not working. If I add a class="required" to the inputs, it works for custom validations, but not for my custom validJson method. But this does not solve my problem as some fields are not required I just need to validate optional values
请问有什么想法吗?
您需要名称属性,如一般准则:
输入元素(验证)需要name属性 没有它,插件将无法工作.通常,名称和ID属性应该 具有相同的值.
The name attribute is required for input elements, the validation plugin doesn't work without it. Usually name and id attributes should have the same value.
因此标记应为
<label for="collection">collection:</label>
<input type="text" id="collection" name="collection">
等