dynamic2.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. <div class="col-lg-8 col-lg-offset-2">
  15. <div class="page-header">
  16. <h2>Dynamic fields</h2>
  17. </div>
  18. <form id="contactForm" method="post" class="form-horizontal" action="target.php">
  19. <div class="form-group">
  20. <label class="col-lg-3 control-label">Phone number</label>
  21. <div class="col-lg-6">
  22. <input class="form-control" type="text" name="phone" />
  23. </div>
  24. <div class="col-lg-3">
  25. <div class="btn-group">
  26. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Add more <span class="caret"></span></button>
  27. <ul class="dropdown-menu" role="menu">
  28. <li><a href="#" class="addPhoneButton" data-name="phone_iphone">iPhone</a></li>
  29. <li><a href="#" class="addPhoneButton" data-name="phone_home">Home</a></li>
  30. <li><a href="#" class="addPhoneButton" data-name="phone_office">Office</a></li>
  31. </ul>
  32. </div>
  33. </div>
  34. </div>
  35. <!-- Template for dynamic field -->
  36. <div class="form-group" id="template" style="display: none;">
  37. <label class="col-lg-3 control-label"></label>
  38. <div class="col-lg-6">
  39. <input class="form-control" type="text" />
  40. </div>
  41. <div class="col-lg-3">
  42. <button type="button" class="btn btn-link removeButton">Remove</button>
  43. </div>
  44. </div>
  45. <div class="form-group">
  46. <div class="col-lg-offset-3 col-lg-3">
  47. <button type="submit" class="btn btn-primary">Submit</button>
  48. </div>
  49. </div>
  50. </form>
  51. </div>
  52. </div>
  53. </div>
  54. <script type="text/javascript">
  55. $(document).ready(function() {
  56. $('.addPhoneButton').on('click', function() {
  57. var $that = $(this),
  58. $parent = $that.parents('.form-group'),
  59. $template = $('#template'),
  60. $newRow = $template.clone().removeAttr('id').insertBefore($template).show();
  61. $that.parent().addClass('disabled');
  62. // Set the label and field name
  63. var fieldName = $that.attr('data-name');
  64. $newRow
  65. .find('.control-label')
  66. .html($that.html())
  67. .end()
  68. .find('input')
  69. .attr('name', fieldName)
  70. .end()
  71. .on('click', '.removeButton', function() {
  72. // Remove field when clicking the Remove button
  73. $('#contactForm').bootstrapValidator('removeField', fieldName);
  74. // Enable the Add button
  75. $that.parent().removeClass('disabled');
  76. // Remove element
  77. $newRow.remove();
  78. });
  79. // Add new field
  80. $('#contactForm').bootstrapValidator('addField', fieldName, {
  81. message: 'The phone number is not valid',
  82. validators: {
  83. digits: {
  84. message: 'The value can contain only digits'
  85. }
  86. }
  87. });
  88. });
  89. $('#contactForm')
  90. .bootstrapValidator({
  91. message: 'This value is not valid',
  92. feedbackIcons: {
  93. valid: 'glyphicon glyphicon-ok',
  94. invalid: 'glyphicon glyphicon-remove',
  95. validating: 'glyphicon glyphicon-refresh'
  96. },
  97. fields: {
  98. phone: {
  99. message: 'The phone number is not valid',
  100. validators: {
  101. notEmpty: {
  102. message: 'The phone number is required'
  103. },
  104. digits: {
  105. message: 'The value can contain only digits'
  106. }
  107. }
  108. }
  109. }
  110. })
  111. .on('error.field.bv', function(e, field, $field) {
  112. console.log(field, $field, '-->error');
  113. })
  114. .on('success.field.bv', function(e, field, $field) {
  115. console.log(field, $field, '-->success');
  116. });
  117. });
  118. </script>
  119. </body>
  120. </html>