selector2.html 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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><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" name="first" type="text" value="0" />
  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" name="second" type="text" value="0" />
  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" name="third" type="text" value="0" />
  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')
  49. .bootstrapValidator({
  50. feedbackIcons: {
  51. valid: 'glyphicon glyphicon-ok',
  52. invalid: 'glyphicon glyphicon-remove',
  53. validating: 'glyphicon glyphicon-refresh'
  54. },
  55. fields: {
  56. percentage: {
  57. selector: '.percent',
  58. validators: {
  59. notEmpty: {
  60. message: 'The percentage is required'
  61. },
  62. callback: {
  63. message: 'The sum must be 100',
  64. callback: function(value, validator) {
  65. var percentage = validator.getFieldElements('percentage'),
  66. length = percentage.length,
  67. sum = 0;
  68. for (var i = 0; i < length; i++) {
  69. sum += parseFloat($(percentage[i]).val());
  70. }
  71. if (sum == 100) {
  72. validator.updateStatus('percentage', 'VALID', 'callback');
  73. return true;
  74. }
  75. return false;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. });
  82. });
  83. </script>
  84. </body>
  85. </html>