form-wizard.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. var FormWizard = function () {
  2. return {
  3. //main function to initiate the module
  4. init: function () {
  5. if (!jQuery().bootstrapWizard) {
  6. return;
  7. }
  8. function format(state) {
  9. if (!state.id) return state.text; // optgroup
  10. return "<img class='flag' src='assets/img/flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
  11. }
  12. $("#country_list").select2({
  13. placeholder: "Select",
  14. allowClear: true,
  15. formatResult: format,
  16. formatSelection: format,
  17. escapeMarkup: function (m) {
  18. return m;
  19. }
  20. });
  21. var form = $('#submit_form');
  22. var error = $('.alert-error', form);
  23. var success = $('.alert-success', form);
  24. form.validate({
  25. doNotHideMessage: true, //this option enables to show the error/success messages on tab switch.
  26. errorElement: 'span', //default input error message container
  27. errorClass: 'validate-inline', // default input error message class
  28. focusInvalid: false, // do not focus the last invalid input
  29. rules: {
  30. //account
  31. username: {
  32. minlength: 5,
  33. required: true
  34. },
  35. password: {
  36. minlength: 5,
  37. required: true
  38. },
  39. rpassword: {
  40. minlength: 5,
  41. required: true,
  42. equalTo: "#submit_form_password"
  43. },
  44. //profile
  45. fullname: {
  46. required: true
  47. },
  48. email: {
  49. required: true,
  50. email: true
  51. },
  52. phone: {
  53. required: true
  54. },
  55. gender: {
  56. required: true
  57. },
  58. address: {
  59. required: true
  60. },
  61. city: {
  62. required: true
  63. },
  64. country: {
  65. required: true
  66. },
  67. //payment
  68. card_name: {
  69. required: true
  70. },
  71. card_number: {
  72. minlength: 16,
  73. maxlength: 16,
  74. required: true
  75. },
  76. card_cvc: {
  77. digits: true,
  78. required: true,
  79. minlength: 3,
  80. maxlength: 4
  81. },
  82. card_expiry_mm: {
  83. digits: true,
  84. required: true
  85. },
  86. card_expiry_yyyy: {
  87. digits: true,
  88. required: true
  89. },
  90. 'payment[]': {
  91. required: true,
  92. minlength: 1
  93. }
  94. },
  95. messages: { // custom messages for radio buttons and checkboxes
  96. 'payment[]': {
  97. required: "Please select at least one option",
  98. minlength: jQuery.format("Please select at least one option")
  99. }
  100. },
  101. errorPlacement: function (error, element) { // render error placement for each input type
  102. if (element.attr("name") == "gender") { // for uniform radio buttons, insert the after the given container
  103. error.addClass("no-left-padding").insertAfter("#form_gender_error");
  104. } else if (element.attr("name") == "payment[]") { // for uniform radio buttons, insert the after the given container
  105. error.addClass("no-left-padding").insertAfter("#form_payment_error");
  106. } else {
  107. error.insertAfter(element); // for other inputs, just perform default behavoir
  108. }
  109. },
  110. invalidHandler: function (event, validator) { //display error alert on form submit
  111. success.hide();
  112. error.show();
  113. App.scrollTo(error, -200);
  114. },
  115. highlight: function (element) { // hightlight error inputs
  116. $(element)
  117. .closest('.help-inline').removeClass('ok'); // display OK icon
  118. $(element)
  119. .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
  120. },
  121. unhighlight: function (element) { // revert the change dony by hightlight
  122. $(element)
  123. .closest('.control-group').removeClass('error'); // set error class to the control group
  124. },
  125. success: function (label) {
  126. if (label.attr("for") == "gender" || label.attr("for") == "payment[]") { // for checkboxes and radip buttons, no need to show OK icon
  127. label
  128. .closest('.control-group').removeClass('error').addClass('success');
  129. label.remove(); // remove error label here
  130. } else { // display success icon for other inputs
  131. label
  132. .addClass('valid ok') // mark the current input as valid and display OK icon
  133. .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
  134. }
  135. },
  136. submitHandler: function (form) {
  137. success.show();
  138. error.hide();
  139. //add here some ajax code to submit your form or just call form.submit() if you want to submit the form without ajax
  140. }
  141. });
  142. var displayConfirm = function() {
  143. $('.display-value', form).each(function(){
  144. var input = $('[name="'+$(this).attr("data-display")+'"]', form);
  145. if (input.is(":text") || input.is("textarea")) {
  146. $(this).html(input.val());
  147. } else if (input.is("select")) {
  148. $(this).html(input.find('option:selected').text());
  149. } else if (input.is(":radio") && input.is(":checked")) {
  150. $(this).html(input.attr("data-title"));
  151. } else if ($(this).attr("data-display") == 'card_expiry') {
  152. $(this).html($('[name="card_expiry_mm"]', form).val() + '/' + $('[name="card_expiry_yyyy"]', form).val());
  153. } else if ($(this).attr("data-display") == 'payment') {
  154. var payment = [];
  155. $('[name="payment[]"]').each(function(){
  156. payment.push($(this).attr('data-title'));
  157. });
  158. $(this).html(payment.join("<br>"));
  159. }
  160. });
  161. }
  162. // default form wizard
  163. $('#form_wizard_1').bootstrapWizard({
  164. 'nextSelector': '.button-next',
  165. 'previousSelector': '.button-previous',
  166. onTabClick: function (tab, navigation, index) {
  167. alert('on tab click disabled');
  168. return false;
  169. },
  170. onNext: function (tab, navigation, index) {
  171. success.hide();
  172. error.hide();
  173. if (form.valid() == false) {
  174. return false;
  175. }
  176. var total = navigation.find('li').length;
  177. var current = index + 1;
  178. // set wizard title
  179. $('.step-title', $('#form_wizard_1')).text('Step ' + (index + 1) + ' of ' + total);
  180. // set done steps
  181. jQuery('li', $('#form_wizard_1')).removeClass("done");
  182. var li_list = navigation.find('li');
  183. for (var i = 0; i < index; i++) {
  184. jQuery(li_list[i]).addClass("done");
  185. }
  186. if (current == 1) {
  187. $('#form_wizard_1').find('.button-previous').hide();
  188. } else {
  189. $('#form_wizard_1').find('.button-previous').show();
  190. }
  191. if (current >= total) {
  192. $('#form_wizard_1').find('.button-next').hide();
  193. $('#form_wizard_1').find('.button-submit').show();
  194. displayConfirm();
  195. } else {
  196. $('#form_wizard_1').find('.button-next').show();
  197. $('#form_wizard_1').find('.button-submit').hide();
  198. }
  199. App.scrollTo($('.page-title'));
  200. },
  201. onPrevious: function (tab, navigation, index) {
  202. success.hide();
  203. error.hide();
  204. var total = navigation.find('li').length;
  205. var current = index + 1;
  206. // set wizard title
  207. $('.step-title', $('#form_wizard_1')).text('Step ' + (index + 1) + ' of ' + total);
  208. // set done steps
  209. jQuery('li', $('#form_wizard_1')).removeClass("done");
  210. var li_list = navigation.find('li');
  211. for (var i = 0; i < index; i++) {
  212. jQuery(li_list[i]).addClass("done");
  213. }
  214. if (current == 1) {
  215. $('#form_wizard_1').find('.button-previous').hide();
  216. } else {
  217. $('#form_wizard_1').find('.button-previous').show();
  218. }
  219. if (current >= total) {
  220. $('#form_wizard_1').find('.button-next').hide();
  221. $('#form_wizard_1').find('.button-submit').show();
  222. } else {
  223. $('#form_wizard_1').find('.button-next').show();
  224. $('#form_wizard_1').find('.button-submit').hide();
  225. }
  226. App.scrollTo($('.page-title'));
  227. },
  228. onTabShow: function (tab, navigation, index) {
  229. var total = navigation.find('li').length;
  230. var current = index + 1;
  231. var $percent = (current / total) * 100;
  232. $('#form_wizard_1').find('.bar').css({
  233. width: $percent + '%'
  234. });
  235. }
  236. });
  237. $('#form_wizard_1').find('.button-previous').hide();
  238. $('#form_wizard_1 .button-submit').click(function () {
  239. alert('Finished! Hope you like it :)');
  240. }).hide();
  241. }
  242. };
  243. }();