2010-11-02 59 views
0

我正在爲表單使用jQuery驗證插件...請參閱下面的完整代碼。雖然下面的內容是檢查需要的項目名稱,但它不檢查長度,並允許提交。看到有什麼不對?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> 
+0

謝謝,我還發現了有錯誤時運行的「invalidHandler」方法。當事情有效時是否有任何處理程序或事件運行?我希望我的提交按鈕淡出,直到所有事情都通過驗證,想法? – TheExit 2010-11-02 20:26:17

+0

查看我的第二個問題的更新答案。 – 2010-11-02 20:51:27

回答

1

更改minLengthminlength(用小寫字母 「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

相關問題