jQuery Validation Plugin - 显示自定义消息

时间:2010-11-02 20:07:42

标签: jquery jquery-plugins jquery-validate

我正在为表单使用jQuery验证插件...请参阅下面的完整代码。虽然以下是检查所需的项目名称,但它不是检查长度,并允许提交。看错了什么?

<form method="post" id="new_space" data-remote="true" class="new_space" action="/spaces" accept-charset="UTF-8">
          <label for="space_name">Name</label><br>
          <input type="text" size="30" name="space[name]" id="space_name" class="text_field">
</form>

<script type="text/javascript">
$(document).ready(function () {
    $("#new_space").validate({
        errorLabelContainer: "#ui-dialog-errors",
        wrapper: "p",
        errorClass: "error",
        invalidHandler: function() {
            $("#ui-dialog-errors").hide().fadeIn();
        },
        rules: {
            "space[name]":{required: true, minLength: 4}
        },
        messages: {
            "space[name]":{ required: "Project Name required!", minLength: "Project Name's need 4+ characters" }
        }
    });
});
</script>

1 个答案:

答案 0 :(得分:1)

minLength更改为minlength(小写“l”)应该修复它。


对于您的第二个问题,假设form_submit是您提交按钮的id,这应该可以解决问题:

// Disable the submit button when first loaded
$("#form_submit").attr("disabled", "disabled");
$("input").change(function() {
    // Check if the form validates
    if($("#new_space").valid()) {
        // Enable the button
        $("#form_submit").removeAttr("disabled");
    }
});

JSFiddle上查看。

相关问题