顯示具有 jQuery Validator 標籤的文章。 顯示所有文章
顯示具有 jQuery Validator 標籤的文章。 顯示所有文章

星期日, 12月 21, 2014

[jQuery Plugin] required depends 讓驗證欄位基於其他特地條件成立

如果你使用jquery validator要讓required規則依特地條件成立才驗證的話, 可以使用呼叫的depends方法,並自訂規則:D
$('#ProspectDtlFrm').validate({ 
    rules: {
        prsptEmail: {
            required: {
                depends: function(element) {
                    return ($('#prsptHomePhone').val() == '' && 
                            $('#prsptBusinessPhone').val() == '' && 
                            $('#prsptMobilePhone').val() == '');
                }
            }
        }    
    }, 
    messages: { 
        prsptEmail: 'Please enter your first name'
    } 
});

Reference
jquery validate depends rule

星期四, 12月 04, 2014

[jquery plugin] jquery validator 驗證多個選取盒checkbox

常用的需求,怕忘記直接寫個範例記一下。
以下範例介紹如何透過jquery validator來驗證多個checkboxes,
可以透過minlength參數來指定選取數量

HTML

欲代理區域:
                        <input type="checkbox" id="north" name="area" value="north" /> 北部
                        <input type="checkbox" id="central" name="area" value="central" /> 中部
                        <input type="checkbox" id="south" name="area" value="south" /> 南部
                        <input type="checkbox" id="east" name="area" value="east" /> 東部


JS範例

$validatedContact
   = $("#contactForm").validate({
    rules: {

     //共同 checkbox
     area: {
      required: true
      ,minlength: 2
     }
},
messages:{
area: {
      required: "區域未選取"
      ,minlength: "至少選取二個區域"
     }
},
 submitHandler: function(form) {
}
});


結果



[jQuery plugin] 移除已初始化過的jquery validator

今天有一個頁面需求,需求在切換不同type的時候,變更不同的form的顯示的欄位, 為了方便就是重設jquery validator,可以透過以下範例重設form的validator

範例

$("#contactForm").data("validator", null);
  $("#contactForm").unbind("validate");
  $("#contactForm").removeData("validator")
  $("#contactForm").unbind("submit");//必需移除submit的行為,不然會多重綁定

星期日, 8月 17, 2014

[jQuery plugin] 使用同一個驗證規則來驗證多個textobx欄位

今天有個畫面需求,需用驗證多個textbox (也可以動態產生),使用同一個規則。 一般來說jquery validator只會驗證一個元素(假設你有多個name故意一樣,也只有一個會被驗證)

以下這個StackoverFlow討論串有提供另一個解法:

http://stackoverflow.com/questions/15739390/validating-multiple-textbox-having-same-name-with-jquery-validator-validates-onl



星期五, 5月 24, 2013

[jQuery plugins] 超激推 qtip plugin,讓錯誤訊息的位置不會撐爆XD

jquery validate是一個幾乎每個網站都會採用的外掛,
但是常常會因為錯誤訊息讓版面整個錯亂,
最近為了這個問題,找到了qtip2這個外掛,
強力的支援各種jquery plugin的tooptip外掛。

http://craigsworks.com/projects/qtip2/

為了方便使用就支接寫了一個擴充的方法,避免專案太多地方引用重覆的程式碼
原則上只是把範例的程式包起來而已...XD

//移除qtip2
 $.validator.prototype.qtipDestory = function(element){
      if( $(element).valid()){
       $(element).filter(".valid").qtip("destroy"); 
      }
    };
//顯示qtip2
$.validator.prototype.qtip = function(error,element,my,at){
  // Set positioning based on the elements position in the form
  var elem = $(element),  
  corners = ["right center", "bottom left"],
  flipIt = elem.parents("span.right").length > 0;

  if(my == "undefined" && at != "undefined"){
   corners = [my,at];
  }
  
  // Check we have a valid error message
  if(!error.is(":empty")) {
    // Apply the tooltip only if it isn"t valid
    elem.filter(":not(.valid)").qtip({
     overwrite: false,
     content: error,
     position: {
      my: corners[ flipIt ? 0 : 1 ],
      at: corners[ flipIt ? 1 : 0 ],
      viewport: $(window)
     },
     show: {
      event: false,
      ready: true
     },
     hide: false,
     style: {
      classes: "qtip-red" // Make it red... the classic error colour!
     }
    })

    // If we have a tooltip on this element already, just update its content
    .qtip("option", "content.text", error);
  } 
  // If the error is empty, remove the qTip
  else { elem.qtip("destroy"); }
 }

接著就照老樣子在errorPlacement補上一刀
...省略
errorPlacement:function(error,element){  
  $your_validated.qtip(error,element);
},
//驗證成功會把qtip消除
success:$.noop,

有時候你需要手動清除qtip(自已遇到關掉dialog要手動清除qtip)
//別忘了reset
$your_validated.resetForm();
$(".qtip").each(function(){
   $(this).data("qtip").destroy();
})

星期日, 11月 18, 2012

[jQuery plugin] ajaxForm 與 validator 整合範例

時常會需要利用到jquery form與 validator 二個元件整合的應用。 把範例記錄一下 :D 範例網址:
注意在submithandler事件裡面要改用ajaxSubmit的方法而不是ajaxForm。

http://jquery.bassistance.de/validate/demo/ajaxSubmit-intergration-demo.html

jQuery(function() {
  // show a simple loading indicator
  var loader = jQuery('
loading...
') .css({position: "relative", top: "1em", left: "25em", display: "inline"}) .appendTo("body") .hide(); jQuery().ajaxStart(function() { loader.show(); }).ajaxStop(function() { loader.hide(); }).ajaxError(function(a, b, e) { throw e; }); var v = jQuery("#form").validate({ submitHandler: function(form) { jQuery(form).ajaxSubmit({ target: "#result" }); } }); jQuery("#reset").click(function() { v.resetForm(); }); });

星期五, 11月 09, 2012

[jQuery plugin] 如何reset from

jquery validator常常submit完要重新清除,常用到的語法。
在此記錄一下。

$(form)[0].reset();

[jQuery plugin] Validator 如何在選擇元素select驗證

常常會用到的validator於選擇元素上的驗證,原來也是加個required就好,記一下 XD

$("#bankForm").validate({
   rules:{
    
     citys:{
     required:true
     }
                     
});
最後發現請選擇的option的value要記得設"",不然會無法動作。

                               
                           

星期六, 9月 15, 2012

[jQuery plugin] jquery Validator表單驗證器 番外篇(進階一點的需求吧XD)

Validator真是好物呀,先前有整理一個入學篇,覺得篇福愈來愈長了,
遇到的新問題就記在這篇吧!!

Q.如何指定要特地驗證某一個欄位
有時候你只想要透過別的動作來檢查某個欄位,可以使用valid方法,回傳值為1:驗證通過,0:驗證失敗

var validateForMoney = $("#borrowMoney").valid()//1:ok,0:false
$.console("validateForMoney:" + validateForMoney);
var validateForRate = $("#borrowRate").valid();
$.console("validateForRate:" + validateForRate);

Q.取得全部驗證失敗的欄位有哪些?
//透過numberOfInvalids方法就會回傳數字了,$validatedPostBorrow是我驗證器的變數
$.console("invalid fields:" + $validatedPostBorrow.numberOfInvalids());


Reference:
jQuery Validate Plugin - Trigger validation of single field

星期日, 7月 15, 2012

[jQuery plugin] Validator 驗證多個Radio跟checkbox範例

今天遇到要檢查多個checkbox至少要選一下就順便記錄一下。
只要將你的checkbox內的name屬性設為 <你取的名字>[]
如下範例 candidate_usr[],記得要加上陣列的符號[]

 $("#candidateForm").validate({
    rules:{
     'candidate_usr[]':{
      required:true
     }
    },
    messages:{
     'candidate_usr[]':{
      required: "請至少選擇一名委託者"
     }
    },
    submitHandler: function(form) {
     //get candicates 
     var $checkboxes 
      = $("#candidateForm").find("input:checkbox:checked").each(
       function(i,cbObj){
        var selectedExpertID = $(cbObj).val();
        $.console("selected id:" + selectedExpertID);
       });
    
    }
  });

Reference:
http://jquery.bassistance.de/validate/demo/radio-checkbox-select-demo.html

星期三, 3月 28, 2012

[jQuery plugin] Validator不透過 submit來觸發檢查欄位

今天遇到一個validator ui上的問題, 由於textbox加了combobox的plugin後,將它放入div裡包起來,會有異常的bug。 把原本驗證textbox拿到form的外面。 所以另外用一個元素來觸發驗證的動作!! 範例如下:


Triggers element validation programmatically.
 //because quota text box position is out of form
 $("#quota_input").change(function(){
  //do validation by changing value in the text box
  $("#filesizeForm").validate().element("#quotaType");
 });

星期六, 11月 26, 2011

其他你感興趣的文章

Related Posts with Thumbnails