selector2.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-1.10.2.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><code>selector</code> example</h2>
  18. </div>
  19. <form id="sumForm" method="post" class="form-horizontal" action="target.php">
  20. <div class="form-group">
  21. <label class="col-lg-3 control-label">Percentage</label>
  22. <div class="col-lg-5">
  23. <input class="form-control percent" type="text" />
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <div class="col-lg-offset-3 col-lg-5">
  28. <input class="form-control percent" type="text" />
  29. </div>
  30. </div>
  31. <div class="form-group">
  32. <div class="col-lg-offset-3 col-lg-5">
  33. <input class="form-control percent" type="text" />
  34. </div>
  35. </div>
  36. <div class="form-group">
  37. <div class="col-lg-9 col-lg-offset-3">
  38. <button type="submit" class="btn btn-primary">Validate</button>
  39. </div>
  40. </div>
  41. </form>
  42. </div>
  43. </section>
  44. </div>
  45. </div>
  46. <script type="text/javascript">
  47. $(document).ready(function() {
  48. $('#sumForm').bootstrapValidator({
  49. feedbackIcons: {
  50. valid: 'glyphicon glyphicon-ok',
  51. invalid: 'glyphicon glyphicon-remove',
  52. validating: 'glyphicon glyphicon-refresh'
  53. },
  54. fields: {
  55. 'percentage': {
  56. selector: '.percent',
  57. validators: {
  58. notEmpty: {
  59. message: 'The percentage is required'
  60. },
  61. callback: {
  62. message: 'The sum must be 100',
  63. callback: function(value, validator) {
  64. var percentage = validator.getFieldElements('percentage'),
  65. length = percentage.length,
  66. sum = 0;
  67. for (var i = 0; i < length; i++) {
  68. sum += parseFloat($(percentage[i]).val());
  69. }
  70. if (sum == 100) {
  71. validator.updateStatus('percentage', 'VALID', 'callback');
  72. return true;
  73. }
  74. return false;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. });
  81. });
  82. </script>
  83. </body>
  84. </html>