暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

form-wizard-validation.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Form Wizard
  3. */
  4. 'use strict';
  5. (function () {
  6. const select2 = $('.select2'),
  7. selectPicker = $('.selectpicker');
  8. // Wizard Validation
  9. // --------------------------------------------------------------------
  10. const wizardValidation = document.querySelector('#wizard-validation');
  11. if (typeof wizardValidation !== undefined && wizardValidation !== null) {
  12. // Wizard form
  13. const wizardValidationForm = wizardValidation.querySelector('#wizard-validation-form');
  14. // Wizard steps
  15. const wizardValidationFormStep1 = wizardValidationForm.querySelector('#account-details-validation');
  16. const wizardValidationFormStep2 = wizardValidationForm.querySelector('#personal-info-validation');
  17. const wizardValidationFormStep3 = wizardValidationForm.querySelector('#social-links-validation');
  18. // Wizard next prev button
  19. const wizardValidationNext = [].slice.call(wizardValidationForm.querySelectorAll('.btn-next'));
  20. const wizardValidationPrev = [].slice.call(wizardValidationForm.querySelectorAll('.btn-prev'));
  21. const validationStepper = new Stepper(wizardValidation, {
  22. linear: true
  23. });
  24. // Account details
  25. const FormValidation1 = FormValidation.formValidation(wizardValidationFormStep1, {
  26. fields: {
  27. formValidationUsername: {
  28. validators: {
  29. notEmpty: {
  30. message: 'The name is required'
  31. },
  32. stringLength: {
  33. min: 6,
  34. max: 30,
  35. message: 'The name must be more than 6 and less than 30 characters long'
  36. },
  37. regexp: {
  38. regexp: /^[a-zA-Z0-9 ]+$/,
  39. message: 'The name can only consist of alphabetical, number and space'
  40. }
  41. }
  42. },
  43. formValidationEmail: {
  44. validators: {
  45. notEmpty: {
  46. message: 'The Email is required'
  47. },
  48. emailAddress: {
  49. message: 'The value is not a valid email address'
  50. }
  51. }
  52. },
  53. formValidationPass: {
  54. validators: {
  55. notEmpty: {
  56. message: 'The password is required'
  57. }
  58. }
  59. },
  60. formValidationConfirmPass: {
  61. validators: {
  62. notEmpty: {
  63. message: 'The Confirm Password is required'
  64. },
  65. identical: {
  66. compare: function () {
  67. return wizardValidationFormStep1.querySelector('[name="formValidationPass"]').value;
  68. },
  69. message: 'The password and its confirm are not the same'
  70. }
  71. }
  72. }
  73. },
  74. plugins: {
  75. trigger: new FormValidation.plugins.Trigger(),
  76. bootstrap5: new FormValidation.plugins.Bootstrap5({
  77. // Use this for enabling/changing valid/invalid class
  78. // eleInvalidClass: '',
  79. eleValidClass: '',
  80. rowSelector: '.form-control-validation'
  81. }),
  82. autoFocus: new FormValidation.plugins.AutoFocus(),
  83. submitButton: new FormValidation.plugins.SubmitButton()
  84. },
  85. init: instance => {
  86. instance.on('plugins.message.placed', function (e) {
  87. //* Move the error message out of the `input-group` element
  88. if (e.element.parentElement.classList.contains('input-group')) {
  89. e.element.parentElement.insertAdjacentElement('afterend', e.messageElement);
  90. }
  91. });
  92. }
  93. }).on('core.form.valid', function () {
  94. // Jump to the next step when all fields in the current step are valid
  95. validationStepper.next();
  96. });
  97. // Personal info
  98. const FormValidation2 = FormValidation.formValidation(wizardValidationFormStep2, {
  99. fields: {
  100. formValidationFirstName: {
  101. validators: {
  102. notEmpty: {
  103. message: 'The first name is required'
  104. }
  105. }
  106. },
  107. formValidationLastName: {
  108. validators: {
  109. notEmpty: {
  110. message: 'The last name is required'
  111. }
  112. }
  113. },
  114. formValidationCountry: {
  115. validators: {
  116. notEmpty: {
  117. message: 'The Country is required'
  118. }
  119. }
  120. },
  121. formValidationLanguage: {
  122. validators: {
  123. notEmpty: {
  124. message: 'The Languages is required'
  125. }
  126. }
  127. }
  128. },
  129. plugins: {
  130. trigger: new FormValidation.plugins.Trigger(),
  131. bootstrap5: new FormValidation.plugins.Bootstrap5({
  132. // Use this for enabling/changing valid/invalid class
  133. // eleInvalidClass: '',
  134. eleValidClass: '',
  135. rowSelector: '.form-control-validation'
  136. }),
  137. autoFocus: new FormValidation.plugins.AutoFocus(),
  138. submitButton: new FormValidation.plugins.SubmitButton()
  139. }
  140. }).on('core.form.valid', function () {
  141. // Jump to the next step when all fields in the current step are valid
  142. validationStepper.next();
  143. });
  144. // Bootstrap Select (i.e Language select)
  145. if (selectPicker.length) {
  146. selectPicker.each(function () {
  147. var $this = $(this);
  148. $this.selectpicker().on('change', function () {
  149. FormValidation2.revalidateField('formValidationLanguage');
  150. });
  151. });
  152. }
  153. // select2
  154. if (select2.length) {
  155. select2.each(function () {
  156. var $this = $(this);
  157. $this.wrap('<div class="position-relative"></div>');
  158. $this
  159. .select2({
  160. placeholder: 'Select an country',
  161. dropdownParent: $this.parent()
  162. })
  163. .on('change', function () {
  164. // Revalidate the color field when an option is chosen
  165. FormValidation2.revalidateField('formValidationCountry');
  166. });
  167. });
  168. }
  169. // Social links
  170. const FormValidation3 = FormValidation.formValidation(wizardValidationFormStep3, {
  171. fields: {
  172. formValidationTwitter: {
  173. validators: {
  174. notEmpty: {
  175. message: 'The Twitter URL is required'
  176. },
  177. uri: {
  178. message: 'The URL is not proper'
  179. }
  180. }
  181. },
  182. formValidationFacebook: {
  183. validators: {
  184. notEmpty: {
  185. message: 'The Facebook URL is required'
  186. },
  187. uri: {
  188. message: 'The URL is not proper'
  189. }
  190. }
  191. },
  192. formValidationGoogle: {
  193. validators: {
  194. notEmpty: {
  195. message: 'The Google URL is required'
  196. },
  197. uri: {
  198. message: 'The URL is not proper'
  199. }
  200. }
  201. },
  202. formValidationLinkedIn: {
  203. validators: {
  204. notEmpty: {
  205. message: 'The LinkedIn URL is required'
  206. },
  207. uri: {
  208. message: 'The URL is not proper'
  209. }
  210. }
  211. }
  212. },
  213. plugins: {
  214. trigger: new FormValidation.plugins.Trigger(),
  215. bootstrap5: new FormValidation.plugins.Bootstrap5({
  216. // Use this for enabling/changing valid/invalid class
  217. // eleInvalidClass: '',
  218. eleValidClass: '',
  219. rowSelector: '.form-control-validation'
  220. }),
  221. autoFocus: new FormValidation.plugins.AutoFocus(),
  222. submitButton: new FormValidation.plugins.SubmitButton()
  223. }
  224. }).on('core.form.valid', function () {
  225. // You can submit the form
  226. // wizardValidationForm.submit()
  227. // or send the form data to server via an Ajax request
  228. // To make the demo simple, I just placed an alert
  229. alert('Submitted..!!');
  230. });
  231. wizardValidationNext.forEach(item => {
  232. item.addEventListener('click', event => {
  233. // When click the Next button, we will validate the current step
  234. switch (validationStepper._currentIndex) {
  235. case 0:
  236. FormValidation1.validate();
  237. break;
  238. case 1:
  239. FormValidation2.validate();
  240. break;
  241. case 2:
  242. FormValidation3.validate();
  243. break;
  244. default:
  245. break;
  246. }
  247. });
  248. });
  249. wizardValidationPrev.forEach(item => {
  250. item.addEventListener('click', event => {
  251. switch (validationStepper._currentIndex) {
  252. case 2:
  253. validationStepper.previous();
  254. break;
  255. case 1:
  256. validationStepper.previous();
  257. break;
  258. case 0:
  259. default:
  260. break;
  261. }
  262. });
  263. });
  264. }
  265. })();