[jQuery]Validation配合驗證碼(Captcha)

Validation若要即時驗證Captcha需要使用額外的Method
剛好自己要用到
就整理一下供大家和自己參考

先看一下,html碼的部分
<img src="code_num.php" id="getcode_num" title="Refresh Captcha" align="absmiddle" />
<input name="code_num" id="code_num" type="text" />

再來jQuery的部分
$(document).ready(function() {
   $.validator.addMethod('checkCaptcha', function() {
    var bfr = false;
     var code = $('#code_num').val();
     var phpquery = $.ajax({url:'chk_code.php',
       type: 'POST',
       async: false,
       data:'act=num&code=' + code,
       success:function(resp) {
         if (resp == 'true') {
           bfr = true;
         }
       }
     });
   if(bfr){
      return true;
    } else {
     $('#getcode_num').attr('src','code_num.php?' + Math.random());
     return false;
    }
   },'');
   $('#sendFrm').validate({
     onkeyup: false,
     rules:{
        code_num: {
          required: true,
          checkCaptcha: true
        }
     },
     messages:{
        code_num:{
         checkCaptcha: 'Your Captcha response was incorrect. Please try again.'
        }
     }
  });
});
簡單說明一下
就是先增加一個Method(checkCaptcha)
這個Method目的是即時檢查驗證碼
而輸入錯誤的話,就再次更新驗證碼
所以後端也要有一支程式去比對驗證碼
 如果不需即時驗證的話,那其實就用不到這方法了

在驗證時,就要把這Method代入
其它都算基本的格式
引用通告地址: http://www.fase.idv.tw/blog/trackback.php?tbID=122&extra=47bea0
標籤: jQuery
評論: 0 | 引用: 119 | 閱讀: 96565
brass fire pit burners [2018-12-10 02:18 ]
標題: brass fire pit burners
連結: https://www.fark.com/users?login=gasfirepits&togglehtml=1&undefined=View&is_using_js=1
摘要: Get today a great Warming trends fire pit kit currently available and now in stock and at a great price this week only!
bozeman seo company [2018-12-09 15:46 ]
標題: bozeman seo company
連結: https://www.destructoid.com/?name=whitefishseo&a=441439&start=0&chaos=ok&who=me
摘要: Get Services From the wonderful seo services agency that is currently available and at great prices today!
mechanism for clocks [2018-11-29 09:33 ]
標題: mechanism for clocks
連結: https://www.tapatalk.com/groups/motorclockparts/viewtopic.php?f=2&t=2
摘要: Get top quality replacement parts for battery operated clocks that are available and in stock now and on sale get it now, today only!
clock Hands Repair [2018-11-28 03:47 ]
標題: clock Hands Repair
連結: https://www.tapatalk.com/groups/motorclockparts/viewtopic.php?f=2&t=2
摘要: Purchase the best quality clock parts that are available and now available and reasonably priced get it now, today only!
發表評論
暱 稱(*): 密 碼:
網 址: E - mail:
驗證碼(*): 驗證碼圖片
請用滑鼠點選輸入
選 項:
頭 像:
內 容(*):