2013-12-09 62 views
-1

我一直在努力嘗試配置jQuery驗證整個下午,並想知道如果我能得到一些幫助。JQuery自定義驗證(errorClass,errorElement和errorPlacement)

我使用yaml.css作爲我的css框架。這YAML適用於表單的方式是,原來的形式看起來像這樣...

<form id="loginForm" name="loginForm" class="ym-form" action="/ReportingManager/j_spring_security_check" method="post">        
    <div class="ym-fbox"> 
     <label for="j_username">Username<sup class="ym-required">*</sup></label> 
     <input id="j_username" name="j_username" placeholder="enter a value" class="{required:true}" type="text" value="" maxlength="30"/> 
    </div>  
    <input id="loginBtn" type="submit" class="ym-button ym-primary" value="Log in" title="Log in" />      
</form> 

形式與我需要做兩件事情的錯誤提交後....

1 - 添加以下略低於輸入字段(YM-fbox DIV中)...

<p class="ym-message"> jquery error message here </p>

2 - 更改包圍在錯誤我的輸入表單元素添加一個名爲類YM-錯誤父DIV ..

從...

<div class="ym-fbox"> 

到...

<div class="ym-fbox ym-error"> 

我還在猶豫後我已經試過,因爲它不工作,但只是爲了顯示我已經做出了嘗試這個是我迄今爲止所嘗試的...

$().ready(function() { 
    $("form").validate({ 
     ... 
    errorClass:'ym-error', 
    errorElement: 'p', 
    errorPlacement: function(error, element) { 
     error.insertAfter(element); 
     }, 
     highlight: function (element, errorClass, validClass) { 
      $(element).parents("div").addClass(errorClass); 
     },  
     unhighlight: function (element, errorClass, validClass) { 
      $(element).parents("div").removeClass(errorClass); 
     }, 

這不好。有人可以給我一些幫助,大概如何形式驗證方法應該看起來好嗎?

感謝

+0

似乎罰款http://jsfiddle.net/arunpjohny/WZvTx/2/...而不是'父母()''使用'.closest()' –

+0

你對我的原始修改工作很好,只需稍微調整一下。謝謝。 – Richie

+0

「不好」是什麼意思? – Sparky

回答

0

爲他人謀取利益......我解決了這個與阿倫的幫助....

$().ready(function() { 
    $("form").validate({ 
     onblur: false, 
     onchange: false, 
     onkeyup: false, 
     onsubmit: true, 
     submitHandler: function(form) { 
      $(form).removeClass('dirty'); 
      $('button').attr('disabled','disabled'); 
      $('button').addClass('disabled'); 
      form.submit(); 
     }, 
     errorClass:'ym-message', 
     errorElement: 'p', 
     errorPlacement: function(error, element) { 
      error.insertAfter(element); 
     }, 
     highlight: function (element, errorClass, validClass) { 
      $(element).closest("div").addClass("ym-error"); 
     },  
     unhighlight: function (element, errorClass, validClass) { 
      $(element).closest("div").removeClass("ym-error"); 
     },  
     ignore: ".ignore" 
    }); 
}); 
+0

你到底解決了什麼問題? – Sparky

+0

僅供參考 - [請閱讀文檔](http://jqueryvalidation.org/validate)。 'onsubmit:true'無效。由於這是默認狀態,因此您將打破此功能。只能使用'false'或覆蓋功能。否則,請忽略此選項。 – Sparky

+0

謝謝Sparky。使用此代碼,它至少添加了這些類並顯示了我想要的錯誤消息。但我會閱讀doco並更改提交內容。歡呼的提示 – Richie