| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- 'use strict';
-
- (function () {
- // Init custom option check
- window.Helpers.initCustomOptionCheck();
-
- // Bootstrap validation example
- //------------------------------------------------------------------------------------------
- // const flatPickrEL = $('.flatpickr-validation');
- const flatPickrList = [].slice.call(document.querySelectorAll('.flatpickr-validation'));
- // Flat pickr
- if (flatPickrList) {
- flatPickrList.forEach(flatPickr => {
- flatPickr.flatpickr({
- allowInput: true,
- monthSelectorType: 'static'
- });
- });
- }
-
- // Fetch all the forms we want to apply custom Bootstrap validation styles to
- const bsValidationForms = document.querySelectorAll('.needs-validation');
-
- // Loop over them and prevent submission
- Array.prototype.slice.call(bsValidationForms).forEach(function (form) {
- form.addEventListener(
- 'submit',
- function (event) {
- if (!form.checkValidity()) {
- event.preventDefault();
- event.stopPropagation();
- } else {
- // Submit your form
- alert('Submitted!!!');
- }
-
- form.classList.add('was-validated');
- },
- false
- );
- });
- })();
- /**
- * Form Validation (https://formvalidation.io/guide/examples)
- * ? Primary form validation plugin for this template
- * ? In this example we've try to covered as many form inputs as we can.
- * ? Though If we've miss any 3rd party libraries, then refer: https://formvalidation.io/guide/examples/integrating-with-3rd-party-libraries
- */
- //------------------------------------------------------------------------------------------
- document.addEventListener('DOMContentLoaded', function (e) {
- (function () {
- const formValidationExamples = document.getElementById('formValidationExamples'),
- formValidationSelect2Ele = jQuery(formValidationExamples.querySelector('[name="formValidationSelect2"]')),
- formValidationTechEle = jQuery(formValidationExamples.querySelector('[name="formValidationTech"]')),
- formValidationLangEle = formValidationExamples.querySelector('[name="formValidationLang"]'),
- formValidationHobbiesEle = jQuery(formValidationExamples.querySelector('.selectpicker')),
- tech = [
- 'ReactJS',
- 'Angular',
- 'VueJS',
- 'Html',
- 'Css',
- 'Sass',
- 'Pug',
- 'Gulp',
- 'Php',
- 'Laravel',
- 'Python',
- 'Bootstrap',
- 'Material Design',
- 'NodeJS'
- ];
-
- const fv = FormValidation.formValidation(formValidationExamples, {
- fields: {
- formValidationName: {
- validators: {
- notEmpty: {
- message: 'Please enter your name'
- },
- stringLength: {
- min: 6,
- max: 30,
- message: 'The name must be more than 6 and less than 30 characters long'
- },
- regexp: {
- regexp: /^[a-zA-Z0-9 ]+$/,
- message: 'The name can only consist of alphabetical, number and space'
- }
- }
- },
- formValidationEmail: {
- validators: {
- notEmpty: {
- message: 'Please enter your email'
- },
- emailAddress: {
- message: 'The value is not a valid email address'
- }
- }
- },
- formValidationPass: {
- validators: {
- notEmpty: {
- message: 'Please enter your password'
- }
- }
- },
- formValidationConfirmPass: {
- validators: {
- notEmpty: {
- message: 'Please confirm password'
- },
- identical: {
- compare: function () {
- return formValidationExamples.querySelector('[name="formValidationPass"]').value;
- },
- message: 'The password and its confirm are not the same'
- }
- }
- },
- formValidationFile: {
- validators: {
- notEmpty: {
- message: 'Please select the file'
- }
- }
- },
- formValidationDob: {
- validators: {
- notEmpty: {
- message: 'Please select your DOB'
- },
- date: {
- format: 'YYYY/MM/DD',
- message: 'The value is not a valid date'
- }
- }
- },
- formValidationSelect2: {
- validators: {
- notEmpty: {
- message: 'Please select your country'
- }
- }
- },
- formValidationLang: {
- validators: {
- notEmpty: {
- message: 'Please add your language'
- }
- }
- },
- formValidationTech: {
- validators: {
- notEmpty: {
- message: 'Please select technology'
- }
- }
- },
- formValidationHobbies: {
- validators: {
- notEmpty: {
- message: 'Please select your hobbies'
- }
- }
- },
- formValidationBio: {
- validators: {
- notEmpty: {
- message: 'Please enter your bio'
- },
- stringLength: {
- min: 100,
- max: 500,
- message: 'The bio must be more than 100 and less than 500 characters long'
- }
- }
- },
- formValidationGender: {
- validators: {
- notEmpty: {
- message: 'Please select your gender'
- }
- }
- },
- formValidationPlan: {
- validators: {
- notEmpty: {
- message: 'Please select your preferred plan'
- }
- }
- },
- formValidationSwitch: {
- validators: {
- notEmpty: {
- message: 'Please select your preference'
- }
- }
- },
- formValidationCheckbox: {
- validators: {
- notEmpty: {
- message: 'Please confirm our T&C'
- }
- }
- }
- },
- plugins: {
- trigger: new FormValidation.plugins.Trigger(),
- bootstrap5: new FormValidation.plugins.Bootstrap5({
- // Use this for enabling/changing valid/invalid class
- // eleInvalidClass: '',
- eleValidClass: '',
- rowSelector: function (field, ele) {
- // field is the field name & ele is the field element
- switch (field) {
- case 'formValidationName':
- case 'formValidationEmail':
- case 'formValidationPass':
- case 'formValidationConfirmPass':
- case 'formValidationFile':
- case 'formValidationDob':
- case 'formValidationSelect2':
- case 'formValidationLang':
- case 'formValidationTech':
- case 'formValidationHobbies':
- case 'formValidationBio':
- case 'formValidationGender':
- case 'formValidationPlan':
- case 'formValidationSwitch':
- case 'formValidationCheckbox':
- return '.form-control-validation';
- default:
- return '.row';
- }
- }
- }),
- submitButton: new FormValidation.plugins.SubmitButton(),
- // Submit the form when all fields are valid
- defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
- autoFocus: new FormValidation.plugins.AutoFocus()
- },
- init: instance => {
- instance.on('plugins.message.placed', function (e) {
- //* Move the error message out of the `input-group` element
- if (e.element.parentElement.classList.contains('input-group')) {
- // `e.field`: The field name
- // `e.messageElement`: The message element
- // `e.element`: The field element
- e.element.parentElement.insertAdjacentElement('afterend', e.messageElement);
- }
- //* Move the error message out of the `row` element for custom-options
- if (e.element.parentElement.parentElement.classList.contains('custom-option')) {
- e.element.closest('.row').insertAdjacentElement('afterend', e.messageElement);
- }
- });
- }
- });
-
- //? Revalidation third-party libs inputs on change trigger
-
- // Flatpickr
- const flatpickrDate = document.querySelector('[name="formValidationDob"]');
-
- if (flatpickrDate) {
- flatpickrDate.flatpickr({
- enableTime: false,
- // See https://flatpickr.js.org/formatting/
- dateFormat: 'Y/m/d',
- // After selecting a date, we need to revalidate the field
- onChange: function () {
- fv.revalidateField('formValidationDob');
- }
- });
- }
-
- // Select2 (Country)
- if (formValidationSelect2Ele.length) {
- formValidationSelect2Ele.wrap('<div class="position-relative"></div>');
- formValidationSelect2Ele
- .select2({
- placeholder: 'Select country',
- dropdownParent: formValidationSelect2Ele.parent()
- })
- .on('change', function () {
- // Revalidate the color field when an option is chosen
- fv.revalidateField('formValidationSelect2');
- });
- }
-
- // Typeahead
-
- // String Matcher function for typeahead
- const substringMatcher = function (strs) {
- return function findMatches(q, cb) {
- var matches, substrRegex;
- matches = [];
- substrRegex = new RegExp(q, 'i');
- $.each(strs, function (i, str) {
- if (substrRegex.test(str)) {
- matches.push(str);
- }
- });
-
- cb(matches);
- };
- };
-
- // Check if rtl
- if (isRtl) {
- const typeaheadList = [].slice.call(document.querySelectorAll('.typeahead'));
-
- // Flat pickr
- if (typeaheadList) {
- typeaheadList.forEach(typeahead => {
- typeahead.setAttribute('dir', 'rtl');
- });
- }
- }
- formValidationTechEle.typeahead(
- {
- hint: !isRtl,
- highlight: true,
- minLength: 1
- },
- {
- name: 'tech',
- source: substringMatcher(tech)
- }
- );
-
- // Tagify
- let formValidationLangTagify = new Tagify(formValidationLangEle);
- formValidationLangEle.addEventListener('change', onChange);
- function onChange() {
- fv.revalidateField('formValidationLang');
- }
-
- //Bootstrap select
- formValidationHobbiesEle.on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
- fv.revalidateField('formValidationHobbies');
- });
- })();
- });
|