Guest User

Untitled

a guest
Jul 19th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     $('#donateForm').submit(function (e) {
  2.         var ok = true;
  3.         var $go = undefined;
  4.         var check = {
  5.             'anonymous': {
  6.                 'anonymous_email': {
  7.                     'msg': '格式錯誤,範例:[email protected]',
  8.                     'checkFunction': function (text) {
  9.                         if ($('#anonymous_event:checked').length) {
  10.                             return /^.+@.+\..{2,3}$/.test(text);
  11.                         } else {
  12.                             return true;
  13.                         }
  14.                     }
  15.                 },
  16.                 'notice': {
  17.                     'msg': '您必須閱讀並勾選此項目',
  18.                     'checkFunction': function (text) {
  19.                         return !!$('#notice:checked').length;
  20.                     }
  21.                 },
  22.                 'amount': {
  23.                     'msg': '請先選擇捐款方式',
  24.                     'checkFunction': function (text) {
  25.                         var method = $('input[name=method]:checked').val();
  26.  
  27.                         if (!method) {
  28.                             return false;
  29.                         }
  30.  
  31.                         this.msg = '金額必須為介於 100 至 10000 的正整數';
  32.  
  33.                         if (!/^\d+$/.test(text)) {
  34.                             return false;
  35.                         }
  36.  
  37.                         if (parseInt(text) < 100 || parseInt(text) > 10000) {
  38.                             return false;
  39.                         }
  40.  
  41.                         return true;
  42.                     }
  43.                 }
  44.             },
  45.             'onymous': {
  46.                 'name': {
  47.                     'msg': '欄位不得為空',
  48.                     'checkFunction': function (text) {
  49.                         return !!text.length;
  50.                     }
  51.                 },
  52.                 'idn': {
  53.                     'msg': '格式錯誤,範例:A123456789',
  54.                     'checkFunction': function (id) {
  55.                         var tab = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
  56.                         var A1 = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3);
  57.                         var A2 = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5);
  58.                         var Mx = new Array(9, 8, 7, 6, 5, 4, 3, 2, 1, 1);
  59.  
  60.                         if (!/^[A-Z][1-2]\d{8,8}$/i.test(id)) {
  61.                             return false;
  62.                         }
  63.  
  64.                         var i = tab.indexOf(id.charAt(0));
  65.                         var sum = A1[i] + A2[i] * 9;
  66.  
  67.                         for (var j = 1; j <= 9; j++) {
  68.                             var v = parseInt(id.charAt(j));
  69.                             sum += v * Mx[j];
  70.                         }
  71.  
  72.                         if (sum % 10) {
  73.                             return false;
  74.                         }
  75.  
  76.                         return true;
  77.                     }
  78.                 },
  79.                 'mobile': {
  80.                     'msg': '格式錯誤,範例:0987654321',
  81.                     'checkFunction': function (text) {
  82.                         return /^09\d{8,8}$/.test(text);
  83.                     }
  84.                 },
  85.                 'tel': {
  86.                     'msg': '欄位不得為空',
  87.                     'checkFunction': function (text) {
  88.                         return !!text.length;
  89.                     }
  90.                 },
  91.                 'email': {
  92.                     'msg': '格式錯誤,範例:[email protected]',
  93.                     'checkFunction': function (text) {
  94.                         return /^.+@.+\..{2,3}$/.test(text);
  95.                     }
  96.                 },
  97.                 'zipcode': {
  98.                     'msg': '格式錯誤,範例:12345',
  99.                     'checkFunction': function (text) {
  100.                         return /^\d{5,5}$/.test(text);
  101.                     }
  102.                 },
  103.                 'address': {
  104.                     'msg': '欄位不得為空',
  105.                     'checkFunction': function (text) {
  106.                         return !!text.length;
  107.                     }
  108.                 },
  109.                 'receipt': {
  110.                     'msg': '欄位不得為空',
  111.                     'checkFunction': function (text) {
  112.                         if (!text.length) return false;
  113.                         if (!(text.indexOf("號") >= 0)) {
  114.                             this.msg = '請輸入正確地址';
  115.                             return false;
  116.                         }
  117.                         return true;
  118.                     }
  119.                 },
  120.                 'notice': {
  121.                     'msg': '您必須閱讀並勾選此項目',
  122.                     'checkFunction': function (text) {
  123.                         return !!$('#notice:checked').length;
  124.                     }
  125.                 },
  126.                 'amount': {
  127.                     'msg': '請先選擇捐款方式',
  128.                     'checkFunction': function (text) {
  129.                         var method = $('input[name=method]:checked').val();
  130.                         var methodLimit = {
  131.                             1: 30000,
  132.                             2: 30000,
  133.                             4: 30000,
  134.                             8: 20000,
  135.                             9: 20000,
  136.                             11: 100000,
  137.                             12: 20000
  138.                         };
  139.  
  140.                         if (!method) {
  141.                             return false;
  142.                         }
  143.  
  144.                         this.msg = '金額必須為介於 100 至 ' + methodLimit[method] + ' 的正整數';
  145.  
  146.                         if (!/^\d+$/.test(text)) {
  147.                             return false;
  148.                         }
  149.  
  150.                         if (parseInt(text) < 100 || parseInt(text) > methodLimit[method]) {
  151.                             return false;
  152.                         }
  153.  
  154.                         return true;
  155.                     }
  156.                 }
  157.             }
  158.         };
  159.         var checkObj = isAnonymous() ? check.anonymous : check.onymous;
  160.  
  161.         $(this).find('.error').remove();
  162.  
  163.         $.each(checkObj, function (index, value) {
  164.             var $obj = $('input[name=' + index + ']');
  165.  
  166.             if (!value.checkFunction.call(this, $obj.val())) {
  167.                 appendErrorMsg($obj.parent(), value.msg);
  168.                 ok = false;
  169.  
  170.                 if (!$go) {
  171.                     $go = $obj.parents('.box');
  172.                 }
  173.             }
  174.         });
  175.  
  176.         if (!ok) {
  177.             var $page = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html, body');
  178.             $page.stop(true, true).animate({scrollTop: $go.offset().top - 10}, 500);
  179.             return e.preventDefault();
  180.         }
  181.  
  182.         $.ajax({
  183.             url: './index.php?i=donation&amp;action=pay'.replace(/&amp;/ig, '&'),
  184.             type: 'POST',
  185.             dataType: 'HTML',
  186.             data: $('#donateForm').serializeArray(),
  187.             beforeSend: function () {
  188.                 $('#submit').prop('disabled', true);
  189.             },
  190.             success: function (data) {
  191.                 $('<form>')
  192.                     .attr({
  193.                         'action': 'https://www.twv.com.tw/openpay/pay.php'.replace(/&amp;/ig, '&'),
  194.                         'method': 'POST'
  195.                     })
  196.                     .append($(data).find('#pay'))
  197.                     .hide()
  198.                     .appendTo('body')
  199.                     .submit();
  200.             }
  201.         });
  202.  
  203.         return e.preventDefault();
  204.     });
Advertisement
Add Comment
Please, Sign In to add comment