selector.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>BootstrapValidator demo</title>
  5. <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
  6. <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
  7. <script type="text/javascript" src="../vendor/jquery/jquery.min.js"></script>
  8. <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
  9. <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
  10. </head>
  11. <body>
  12. <div class="container">
  13. <div class="row">
  14. <section>
  15. <div class="col-lg-8 col-lg-offset-2">
  16. <div class="page-header">
  17. <h2>Credit card information</h2>
  18. </div>
  19. <form id="paymentForm" method="post" class="form-horizontal" action="target.php">
  20. <div class="form-group">
  21. <label class="col-lg-3 control-label">Card holder</label>
  22. <div class="col-lg-5">
  23. <input type="text" class="form-control" id="cardHolder" />
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <label class="col-lg-3 control-label">Credit card number</label>
  28. <div class="col-lg-5">
  29. <input type="text" class="form-control" id="ccNumber" />
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. <label class="col-lg-3 control-label">Expiration</label>
  34. <div class="col-lg-4">
  35. <input type="text" class="form-control" placeholder="Month" data-stripe="exp-month" />
  36. </div>
  37. <div class="col-lg-4">
  38. <input type="text" class="form-control" placeholder="Year" data-stripe="exp-year" />
  39. </div>
  40. </div>
  41. <div class="form-group">
  42. <label class="col-lg-3 control-label">CVV</label>
  43. <div class="col-lg-2">
  44. <input type="text" class="form-control cvvNumber" name="cvv" />
  45. </div>
  46. </div>
  47. <div class="form-group">
  48. <div class="col-lg-9 col-lg-offset-3">
  49. <button type="submit" class="btn btn-primary">Pay</button>
  50. </div>
  51. </div>
  52. </form>
  53. </div>
  54. </section>
  55. </div>
  56. </div>
  57. <script type="text/javascript">
  58. $(document).ready(function() {
  59. $('#paymentForm').bootstrapValidator({
  60. feedbackIcons: {
  61. valid: 'glyphicon glyphicon-ok',
  62. invalid: 'glyphicon glyphicon-remove',
  63. validating: 'glyphicon glyphicon-refresh'
  64. },
  65. fields: {
  66. cardHolder: {
  67. selector: '#cardHolder',
  68. validators: {
  69. notEmpty: {
  70. message: 'The card holder is required'
  71. },
  72. stringCase: {
  73. message: 'The card holder must contain upper case characters only',
  74. case: 'upper'
  75. }
  76. }
  77. },
  78. ccNumber: {
  79. selector: '#ccNumber',
  80. validators: {
  81. notEmpty: {
  82. message: 'The credit card number is required'
  83. },
  84. creditCard: {
  85. message: 'The credit card number is not valid'
  86. }
  87. }
  88. },
  89. expMonth: {
  90. selector: '[data-stripe="exp-month"]',
  91. validators: {
  92. notEmpty: {
  93. message: 'The expiration month is required'
  94. },
  95. digits: {
  96. message: 'The expiration month can contain digits only'
  97. },
  98. callback: {
  99. message: 'Expired',
  100. callback: function(value, validator) {
  101. value = parseInt(value, 10);
  102. var year = validator.getFieldElements('expYear').val(),
  103. currentMonth = new Date().getMonth() + 1,
  104. currentYear = new Date().getFullYear();
  105. if (value < 0 || value > 12) {
  106. return false;
  107. }
  108. if (year == '') {
  109. return true;
  110. }
  111. year = parseInt(year, 10);
  112. if (year > currentYear || (year == currentYear && value > currentMonth)) {
  113. validator.updateStatus('expYear', 'VALID');
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. }
  120. }
  121. },
  122. expYear: {
  123. selector: '[data-stripe="exp-year"]',
  124. validators: {
  125. notEmpty: {
  126. message: 'The expiration year is required'
  127. },
  128. digits: {
  129. message: 'The expiration year can contain digits only'
  130. },
  131. callback: {
  132. message: 'Expired',
  133. callback: function(value, validator) {
  134. value = parseInt(value, 10);
  135. var month = validator.getFieldElements('expMonth').val(),
  136. currentMonth = new Date().getMonth() + 1,
  137. currentYear = new Date().getFullYear();
  138. if (value < currentYear || value > currentYear + 10) {
  139. return false;
  140. }
  141. if (month == '') {
  142. return false;
  143. }
  144. month = parseInt(month, 10);
  145. if (value > currentYear || (value == currentYear && month > currentMonth)) {
  146. validator.updateStatus('expMonth', 'VALID');
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. }
  153. }
  154. },
  155. cvvNumber: {
  156. selector: '.cvvNumber',
  157. validators: {
  158. notEmpty: {
  159. message: 'The CVV number is required'
  160. },
  161. cvv: {
  162. message: 'The value is not a valid CVV',
  163. creditCardField: 'ccNumber'
  164. }
  165. }
  166. }
  167. }
  168. });
  169. });
  170. </script>
  171. </body>
  172. </html>