jquery验证有效和错误类以形成

时间:2014-05-27 20:00:43

标签: javascript jquery

我正在使用jquery validate插件。我有这样的表格:

<form id="form-test" class="form-inline" role="form">
    <div id="test" class="form-group has-feedback">
        <label class="control-label" for="inputSuccess4">Input with success</label>
        <input type="text" name="prova" id="prova" class="form-control" id="inputSuccess4">
        <span class="glyphicon glyphicon-ok form-control-feedback"></span>
     </div>
 </form>

这是我的jquery验证文件:

$('#form-test').validate({
    rules: {
        prova: {
            required: true,
            minlength: 4,
            maxlength: 7
        }
    },

    submitHandler: function(form) { 
        form.submit();
    }
});

如果输入有效,我需要添加到div(带有id测试)类has-success,如果是无效的has-error。我怎样才能做到这一点?谢谢

2 个答案:

答案 0 :(得分:1)

$('#form-test').validate({
    errorClass: "has-error",
    validClass: "has-success",
    rules: {
        prova: {
            required: true,
            minlength: 4,
            maxlength: 7
        }
    },
    submitHandler: function(form) { 
        form.submit();
    }
});

答案 1 :(得分:0)

如果是if,则只需创建.valid()条件:

isValid = $('#form-test').valid();
    if (isValid) {
        $('#test').addClass('has-success');
    } else {
        $('#test').addClass('has-error');
    }