event3.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. <div class="col-lg-8 col-lg-offset-2">
  15. <div class="page-header">
  16. <h2>Using events</h2>
  17. <p class="lead">The captcha is regenerated if the input is not valid</p>
  18. </div>
  19. <form id="form" method="post" class="form-horizontal" action="target.php">
  20. <div class="form-group">
  21. <label class="col-lg-3 control-label">Full name</label>
  22. <div class="col-lg-5">
  23. <input type="text" class="form-control" name="fullName" />
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <label class="col-lg-3 control-label" id="captchaOperation"></label>
  28. <div class="col-lg-2">
  29. <input type="text" class="form-control" name="captcha" />
  30. </div>
  31. </div>
  32. <div class="form-group">
  33. <div class="col-lg-9 col-lg-offset-3">
  34. <button type="submit" class="btn btn-primary">Submit</button>
  35. </div>
  36. </div>
  37. </form>
  38. </div>
  39. </div>
  40. </div>
  41. <script type="text/javascript">
  42. $(document).ready(function() {
  43. // Generate a simple captcha
  44. function randomNumber(min, max) {
  45. return Math.floor(Math.random() * (max - min + 1) + min);
  46. };
  47. function generateCaptcha() {
  48. $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
  49. };
  50. generateCaptcha();
  51. $('#form')
  52. .bootstrapValidator({
  53. message: 'This value is not valid',
  54. feedbackIcons: {
  55. valid: 'glyphicon glyphicon-ok',
  56. invalid: 'glyphicon glyphicon-remove',
  57. validating: 'glyphicon glyphicon-refresh'
  58. },
  59. fields: {
  60. fullName: {
  61. validators: {
  62. notEmpty: {
  63. message: 'The full name is required'
  64. }
  65. }
  66. },
  67. captcha: {
  68. validators: {
  69. callback: {
  70. message: 'Wrong answer',
  71. callback: function(value, validator) {
  72. var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
  73. return value == sum;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. })
  80. .on('error.form.bv', function(e) {
  81. var $form = $(e.target),
  82. bootstrapValidator = $form.data('bootstrapValidator');
  83. if (!bootstrapValidator.isValidField('captcha')) {
  84. // The captcha is not valid
  85. // Regenerate the captcha
  86. generateCaptcha();
  87. }
  88. });
  89. });
  90. </script>
  91. </body>
  92. </html>