JQuery字段检查

时间:2013-10-21 10:19:26

标签: javascript jquery

我只是想检查来自field和field1的输入是否相同。但我不确定我的错误在哪里。

jQuery代码:

$(document).ready(function () {
    $('#f').validate({ 
        rules: {
            field: {
                nowhitespace: true, 
                minlength: 6,
                required: true
            },
            field11: {
                equalTo: "#field1",
                required: true
            }
        },
        messages: {
        field: {
                required: "required",
                minlength: "min 6."
            },
            field11: {
                required: "required",
            equalTo: "same as field."
            }
        },
        submitHandler: function (form) { 
            javascript:document.f.submit();
        }
    });
});

HTML:

<form name="f" action="/path/to/service" method="POST">
    <table>
        <tr>
            <td><label for="field">Field</label>:&nbsp;&nbsp;</td>
            <td>
                <input class="field" class="pw" type='field' id="field" name='field' size="20" />
            </td>
        </tr>
        <tr>
            <td><label for="field1">field 1</label>:&nbsp;&nbsp;</td>
            <td>
                <input class="field" class="pw" type='field1' id="field1" name='field1' size="20" />
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
            <td colspan='2' align="center">
                <input name="submit" type="submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>

有人能看出我的错吗? thx任何帮助

2 个答案:

答案 0 :(得分:1)

尝试使用rules

rules: {
        field: {
            nowhitespace: true,
            minlength: 6,
            required: true
        },
        field11: {
            equalTo: "#field",// Use "#field" in place of "#field1" 
            required: true
        }
    },

表单标记中添加 id ,因为您使用 $('#f')。validate({ like,

<form name="f" id="f" action="/path/to/service" method="POST">

或者在没有 id 之类的情况下尝试,

$('form[name="f"]').validate({

阅读 equalTo method

答案 1 :(得分:0)

equalTo上的field规则设置为#field1 - 它始终等于自身。其次,在您设置field11的规则中,实际名称为field1。试试这个:

field1: {
    equalTo: "#field",
    required: true
}