2015年11月3日 星期二

【ASP.NET MVC】JQuery validate parameters rule and customer message not working

  1. // 錯誤示範
  2. $("#form1").validate({
  3. rules: {
  4. txtName: "required",
  5. txtAge: {
  6. required: true,
  7. min: 18
  8. }
  9. },
  10. messages: {
  11. required: "*必填欄位",
  12. min: jQuery.validator.format("*年齡須大於{0}")
  13. }
  14. });
在試客戶端驗證時碰到的問題

上面的程式完全沒有反應,但如果改成
  1. // solution 2
  2. $("#form1").validate();
  3.  
  4. $("#txtName").rules("add", {
  5. required: true,
  6. messages: {
  7. required: "*必填欄位"
  8. }
  9. });
  10.  
  11. $("#txtAge").rules("add", {
  12. required: true,
  13. min: 18,
  14. messages: {
  15. required: "*必填欄位",
  16. min: jQuery.validator.format("*年齡須大於{0}")
  17. }
  18. });
就可行(注意,這個寫法 form.validate(...)一定要在加 rule 進來之前)。

View 部分如下
  1. @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "form1", @class = "form1", name = "form1" }))
  2. {
  3. <div class="bs-callout bs-callout-primary">
  4. <h4>Person info form</h4>
  5. <p>Plz fill in the personal info and press submit btn to send data back.</p>
  6. </div>
  7.  
  8. <div class="form-group buffer-bottom">
  9. <div class="input-group">
  10. <span class="input-group-addon same-width-iga">Name</span>
  11. @Html.EditorFor(m => m.Name, new { htmlAttributes = new { placeholder = "Andy", @class = "form-control", id = "txtName" } })
  12. </div>
  13. </div>
  14.  
  15. <div class="form-group buffer-bottom">
  16. <div class="input-group">
  17. <span class="input-group-addon same-width-iga">Age</span>
  18. @Html.EditorFor(m => m.Age, new { htmlAttributes = new { placeholder = 18, @class = "form-control", id = "txtAge" } })
  19. </div>
  20. </div>
  21. <button class="btn btn-default buffer-right">Cancel</button><button class="btn btn-primary" id="btnSubmit">Submit</button>
  22. }

莫名地找了半天才發現 原因

所以
這部分必須是對應 input tag 的 name,但是下面這樣
行不通,Helper 產生出來的 input tag 會跟著 model prop 的名稱走


所以山不轉路轉,把 validate 參數的部分配合 model 改掉
  1. $("#form1").validate({
  2. rules: {
  3. Name: "required",
  4. Age: {
  5. required: true,
  6. min: 18
  7. }
  8. },
  9. messages: {
  10. required: "*必填欄位",
  11. min: jQuery.validator.format("*年齡須大於{0}")
  12. }
  13. });

有點進步,至少驗證有觸發了,不過訊息沒有正確改成客製的

結果問題在於我的想法是錯的,在參數這部分 message 還是必須要個別指定給 input 所以是
  1. $("#form1").validate({
  2. rules: {
  3. Name: "required",
  4. Age: {
  5. required: true,
  6. min: 18
  7. }
  8. },
  9. messages: {
  10. Name: {
  11. required: "*必填欄位"
  12. },
  13. Age: {
  14. required: "*必填欄位",
  15. min: jQuery.validator.format("*年齡須大於{0}")
  16. }
  17. }
  18. });

終於對了

可是我之所以不想用 solution 2 就是希望整個專案的同樣驗證規則只要一份客製訊息就好啊! 參考
  1. $("#form1").validate({
  2. rules: {
  3. Name: "required",
  4. Age: {
  5. required: true,
  6. min: 18
  7. }
  8. }
  9. });
  10.  
  11. $.extend($.validator.messages, {
  12. required: "*必填欄位",
  13. min: $.validator.format("*年齡須大於{0}")
  14. });
改成這樣即可

如果你希望驗證時不要送出可以用 form 的 valid() 就好

先把 button(或 input) 改成 type button,避免它送出
  1. <button class="btn btn-primary" id="btnSubmit" type="button">Submit</button>

然後用
  1. $("#btnSubmit").click(function () {
  2. $("#form1").valid();
  3. });
就行了

如果你需要 client 端客製規則,參考
如果你要完整專案 GitHub

沒有留言:

張貼留言