jquery验证器插件 - 没有验证

时间:2012-04-11 01:07:21

标签: jquery jquery-validate

我知道这很简单(无论如何根据插件页面)

但是我试图使用插件没有成功,这是我的表格:

<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脚本:

$(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方法。但这并不能解决我的问题,因为不需要某些字段我只需要验证可选值

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要名称属性,如the general guidelines:

中所述
  

输入元素验证需要name属性   如果没有它,插件就无法运行。通常名称和id属性应该   具有相同的价值。

所以标记应该是

<label for="collection">collection:</label> 
<input type="text" id="collection" name="collection">

相关问题