Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form-validation.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. 'use strict';
  2. (function () {
  3. // Init custom option check
  4. window.Helpers.initCustomOptionCheck();
  5. // Bootstrap validation example
  6. //------------------------------------------------------------------------------------------
  7. // const flatPickrEL = $('.flatpickr-validation');
  8. const flatPickrList = [].slice.call(document.querySelectorAll('.flatpickr-validation'));
  9. // Flat pickr
  10. if (flatPickrList) {
  11. flatPickrList.forEach(flatPickr => {
  12. flatPickr.flatpickr({
  13. allowInput: true,
  14. monthSelectorType: 'static'
  15. });
  16. });
  17. }
  18. // Fetch all the forms we want to apply custom Bootstrap validation styles to
  19. const bsValidationForms = document.querySelectorAll('.needs-validation');
  20. // Loop over them and prevent submission
  21. Array.prototype.slice.call(bsValidationForms).forEach(function (form) {
  22. form.addEventListener(
  23. 'submit',
  24. function (event) {
  25. if (!form.checkValidity()) {
  26. event.preventDefault();
  27. event.stopPropagation();
  28. } else {
  29. // Submit your form
  30. alert('Submitted!!!');
  31. }
  32. form.classList.add('was-validated');
  33. },
  34. false
  35. );
  36. });
  37. })();
  38. /**
  39. * Form Validation (https://formvalidation.io/guide/examples)
  40. * ? Primary form validation plugin for this template
  41. * ? In this example we've try to covered as many form inputs as we can.
  42. * ? Though If we've miss any 3rd party libraries, then refer: https://formvalidation.io/guide/examples/integrating-with-3rd-party-libraries
  43. */
  44. //------------------------------------------------------------------------------------------
  45. document.addEventListener('DOMContentLoaded', function (e) {
  46. (function () {
  47. const formValidationExamples = document.getElementById('formValidationExamples'),
  48. formValidationSelect2Ele = jQuery(formValidationExamples.querySelector('[name="formValidationSelect2"]')),
  49. formValidationTechEle = jQuery(formValidationExamples.querySelector('[name="formValidationTech"]')),
  50. formValidationLangEle = formValidationExamples.querySelector('[name="formValidationLang"]'),
  51. formValidationHobbiesEle = jQuery(formValidationExamples.querySelector('.selectpicker')),
  52. tech = [
  53. 'ReactJS',
  54. 'Angular',
  55. 'VueJS',
  56. 'Html',
  57. 'Css',
  58. 'Sass',
  59. 'Pug',
  60. 'Gulp',
  61. 'Php',
  62. 'Laravel',
  63. 'Python',
  64. 'Bootstrap',
  65. 'Material Design',
  66. 'NodeJS'
  67. ];
  68. const fv = FormValidation.formValidation(formValidationExamples, {
  69. fields: {
  70. formValidationName: {
  71. validators: {
  72. notEmpty: {
  73. message: 'Please enter your name'
  74. },
  75. stringLength: {
  76. min: 6,
  77. max: 30,
  78. message: 'The name must be more than 6 and less than 30 characters long'
  79. },
  80. regexp: {
  81. regexp: /^[a-zA-Z0-9 ]+$/,
  82. message: 'The name can only consist of alphabetical, number and space'
  83. }
  84. }
  85. },
  86. formValidationEmail: {
  87. validators: {
  88. notEmpty: {
  89. message: 'Please enter your email'
  90. },
  91. emailAddress: {
  92. message: 'The value is not a valid email address'
  93. }
  94. }
  95. },
  96. formValidationPass: {
  97. validators: {
  98. notEmpty: {
  99. message: 'Please enter your password'
  100. }
  101. }
  102. },
  103. formValidationConfirmPass: {
  104. validators: {
  105. notEmpty: {
  106. message: 'Please confirm password'
  107. },
  108. identical: {
  109. compare: function () {
  110. return formValidationExamples.querySelector('[name="formValidationPass"]').value;
  111. },
  112. message: 'The password and its confirm are not the same'
  113. }
  114. }
  115. },
  116. formValidationFile: {
  117. validators: {
  118. notEmpty: {
  119. message: 'Please select the file'
  120. }
  121. }
  122. },
  123. formValidationDob: {
  124. validators: {
  125. notEmpty: {
  126. message: 'Please select your DOB'
  127. },
  128. date: {
  129. format: 'YYYY/MM/DD',
  130. message: 'The value is not a valid date'
  131. }
  132. }
  133. },
  134. formValidationSelect2: {
  135. validators: {
  136. notEmpty: {
  137. message: 'Please select your country'
  138. }
  139. }
  140. },
  141. formValidationLang: {
  142. validators: {
  143. notEmpty: {
  144. message: 'Please add your language'
  145. }
  146. }
  147. },
  148. formValidationTech: {
  149. validators: {
  150. notEmpty: {
  151. message: 'Please select technology'
  152. }
  153. }
  154. },
  155. formValidationHobbies: {
  156. validators: {
  157. notEmpty: {
  158. message: 'Please select your hobbies'
  159. }
  160. }
  161. },
  162. formValidationBio: {
  163. validators: {
  164. notEmpty: {
  165. message: 'Please enter your bio'
  166. },
  167. stringLength: {
  168. min: 100,
  169. max: 500,
  170. message: 'The bio must be more than 100 and less than 500 characters long'
  171. }
  172. }
  173. },
  174. formValidationGender: {
  175. validators: {
  176. notEmpty: {
  177. message: 'Please select your gender'
  178. }
  179. }
  180. },
  181. formValidationPlan: {
  182. validators: {
  183. notEmpty: {
  184. message: 'Please select your preferred plan'
  185. }
  186. }
  187. },
  188. formValidationSwitch: {
  189. validators: {
  190. notEmpty: {
  191. message: 'Please select your preference'
  192. }
  193. }
  194. },
  195. formValidationCheckbox: {
  196. validators: {
  197. notEmpty: {
  198. message: 'Please confirm our T&C'
  199. }
  200. }
  201. }
  202. },
  203. plugins: {
  204. trigger: new FormValidation.plugins.Trigger(),
  205. bootstrap5: new FormValidation.plugins.Bootstrap5({
  206. // Use this for enabling/changing valid/invalid class
  207. // eleInvalidClass: '',
  208. eleValidClass: '',
  209. rowSelector: function (field, ele) {
  210. // field is the field name & ele is the field element
  211. switch (field) {
  212. case 'formValidationName':
  213. case 'formValidationEmail':
  214. case 'formValidationPass':
  215. case 'formValidationConfirmPass':
  216. case 'formValidationFile':
  217. case 'formValidationDob':
  218. case 'formValidationSelect2':
  219. case 'formValidationLang':
  220. case 'formValidationTech':
  221. case 'formValidationHobbies':
  222. case 'formValidationBio':
  223. case 'formValidationGender':
  224. case 'formValidationPlan':
  225. case 'formValidationSwitch':
  226. case 'formValidationCheckbox':
  227. return '.form-control-validation';
  228. default:
  229. return '.row';
  230. }
  231. }
  232. }),
  233. submitButton: new FormValidation.plugins.SubmitButton(),
  234. // Submit the form when all fields are valid
  235. defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
  236. autoFocus: new FormValidation.plugins.AutoFocus()
  237. },
  238. init: instance => {
  239. instance.on('plugins.message.placed', function (e) {
  240. //* Move the error message out of the `input-group` element
  241. if (e.element.parentElement.classList.contains('input-group')) {
  242. // `e.field`: The field name
  243. // `e.messageElement`: The message element
  244. // `e.element`: The field element
  245. e.element.parentElement.insertAdjacentElement('afterend', e.messageElement);
  246. }
  247. //* Move the error message out of the `row` element for custom-options
  248. if (e.element.parentElement.parentElement.classList.contains('custom-option')) {
  249. e.element.closest('.row').insertAdjacentElement('afterend', e.messageElement);
  250. }
  251. });
  252. }
  253. });
  254. //? Revalidation third-party libs inputs on change trigger
  255. // Flatpickr
  256. const flatpickrDate = document.querySelector('[name="formValidationDob"]');
  257. if (flatpickrDate) {
  258. flatpickrDate.flatpickr({
  259. enableTime: false,
  260. // See https://flatpickr.js.org/formatting/
  261. dateFormat: 'Y/m/d',
  262. // After selecting a date, we need to revalidate the field
  263. onChange: function () {
  264. fv.revalidateField('formValidationDob');
  265. }
  266. });
  267. }
  268. // Select2 (Country)
  269. if (formValidationSelect2Ele.length) {
  270. formValidationSelect2Ele.wrap('<div class="position-relative"></div>');
  271. formValidationSelect2Ele
  272. .select2({
  273. placeholder: 'Select country',
  274. dropdownParent: formValidationSelect2Ele.parent()
  275. })
  276. .on('change', function () {
  277. // Revalidate the color field when an option is chosen
  278. fv.revalidateField('formValidationSelect2');
  279. });
  280. }
  281. // Typeahead
  282. // String Matcher function for typeahead
  283. const substringMatcher = function (strs) {
  284. return function findMatches(q, cb) {
  285. var matches, substrRegex;
  286. matches = [];
  287. substrRegex = new RegExp(q, 'i');
  288. $.each(strs, function (i, str) {
  289. if (substrRegex.test(str)) {
  290. matches.push(str);
  291. }
  292. });
  293. cb(matches);
  294. };
  295. };
  296. // Check if rtl
  297. if (isRtl) {
  298. const typeaheadList = [].slice.call(document.querySelectorAll('.typeahead'));
  299. // Flat pickr
  300. if (typeaheadList) {
  301. typeaheadList.forEach(typeahead => {
  302. typeahead.setAttribute('dir', 'rtl');
  303. });
  304. }
  305. }
  306. formValidationTechEle.typeahead(
  307. {
  308. hint: !isRtl,
  309. highlight: true,
  310. minLength: 1
  311. },
  312. {
  313. name: 'tech',
  314. source: substringMatcher(tech)
  315. }
  316. );
  317. // Tagify
  318. let formValidationLangTagify = new Tagify(formValidationLangEle);
  319. formValidationLangEle.addEventListener('change', onChange);
  320. function onChange() {
  321. fv.revalidateField('formValidationLang');
  322. }
  323. //Bootstrap select
  324. formValidationHobbiesEle.on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
  325. fv.revalidateField('formValidationHobbies');
  326. });
  327. })();
  328. });