Internal Site UI
Internal Site UI

FormValidation - Advanced

Overview

FormValidation is one of the best validation library for JavaScript. For more info see the official siteand the Github repository
All input types require the nameattribute to bind the input field for validation.

Date range picker

For more information on Date Range Pickers, please visit the official website.
// Define form element
const form = document.getElementById('kt_docs_formvalidation_daterangepicker');

// Init daterangepicker --- for more info, please visit: https://www.daterangepicker.com/
element.daterangepicker({
autoUpdateInput: false,
});

element.on('apply.daterangepicker', function(ev, picker) {
$(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
});

element.on('cancel.daterangepicker', function(ev, picker) {
$(this).val('');
});

// Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
var validator = FormValidation.formValidation(
form,
{
fields: {
'daterangepicker_input': {
    validators: {
        notEmpty: {
            message: 'Date range input is required'
        }
    }
},
},

plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap: new FormValidation.plugins.Bootstrap5({
    rowSelector: '.fv-row',
    eleInvalidClass: '',
    eleValidClass: ''
})
}
}
);

// Submit button handler
const submitButton = document.getElementById('kt_docs_formvalidation_daterangepicker_submit');
submitButton.addEventListener('click', function (e) {
// Prevent default button action
e.preventDefault();

// Validate form before submit
if (validator) {
validator.validate().then(function (status) {
console.log('validated!');

if (status == 'Valid') {
    // Show loading indication
    submitButton.setAttribute('data-kt-indicator', 'on');

    // Disable button to avoid multiple click
    submitButton.disabled = true;

    // Simulate form submission. For more info check the plugin's official documentation: https://sweetalert2.github.io/
    setTimeout(function () {
        // Remove loading indication
        submitButton.removeAttribute('data-kt-indicator');

        // Enable button
        submitButton.disabled = false;

        // Show popup confirmation
        Swal.fire({
            text: "Form has been successfully submitted!",
            icon: "success",
            buttonsStyling: false,
            confirmButtonText: "Ok, got it!",
            customClass: {
                confirmButton: "btn btn-primary"
            }
        });

        //form.submit(); // Submit form
    }, 2000);
}
});
}
});
<!--begin::Form-->
<form id="kt_docs_formvalidation_daterangepicker" class="form" action="#" autocomplete="off">
<!--begin::Input group-->
<div class="fv-row mb-10">
<!--begin::Label-->
<label class="required fw-semibold fs-6 mb-2">Date Range Picker Input</label>
<!--end::Label-->

<!--begin::Input-->
<input class="form-control form-control-solid" name="daterangepicker_input" placeholder="Pick date range" id="kt_daterangepicker" />
<!--end::Input-->
</div>
<!--end::Input group-->

<!--begin::Actions-->
<button id="kt_docs_formvalidation_daterangepicker_submit" type="submit" class="btn btn-primary">
<span class="indicator-label">
Validation Form
</span>
<span class="indicator-progress">
Please wait... <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
</span>
</button>
<!--end::Actions-->
</form>
<!--end::Form-->

Flatpickr

For more information on Flatpickr, please visit the official website.

Image Input

Allowed file types: png, jpg, jpeg.

Password Meter

Use 8 or more characters with a mix of letters, numbers & symbols.

Select2

For more information on Select2, please visit the official website.
// Define form element
const form = document.getElementById('kt_docs_formvalidation_select2');

// Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
var validator = FormValidation.formValidation(
form,
{
fields: {
'select2_input': {
    validators: {
        notEmpty: {
            message: 'Select2 input is required'
        }
    }
},
},

plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap: new FormValidation.plugins.Bootstrap5({
    rowSelector: '.fv-row',
    eleInvalidClass: '',
    eleValidClass: ''
})
}
}
);

// Revalidate Select2 input. For more info, plase visit the official plugin site: https://select2.org/
$(form.querySelector('[name="select2_input"]')).on('change', function () {
// Revalidate the field when an option is chosen
validator.revalidateField('select2_input');
});


// Submit button handler
const submitButton = document.getElementById('kt_docs_formvalidation_select2_submit');
submitButton.addEventListener('click', function (e) {
// Prevent default button action
e.preventDefault();

// Validate form before submit
if (validator) {
validator.validate().then(function (status) {
console.log('validated!');

if (status == 'Valid') {
    // Show loading indication
    submitButton.setAttribute('data-kt-indicator', 'on');

    // Disable button to avoid multiple click
    submitButton.disabled = true;

    // Simulate form submission. For more info check the plugin's official documentation: https://sweetalert2.github.io/
    setTimeout(function () {
        // Remove loading indication
        submitButton.removeAttribute('data-kt-indicator');

        // Enable button
        submitButton.disabled = false;

        // Show popup confirmation
        Swal.fire({
            text: "Form has been successfully submitted!",
            icon: "success",
            buttonsStyling: false,
            confirmButtonText: "Ok, got it!",
            customClass: {
                confirmButton: "btn btn-primary"
            }
        });

        //form.submit(); // Submit form
    }, 2000);
}
});
}
});
<!--begin::Form-->
<form id="kt_docs_formvalidation_select2" class="form" action="#" autocomplete="off">
<!--begin::Input group--->
<div class="fv-row mb-10">
<!--begin::Label-->
<label class="required form-label fs-6 mb-2">Select2 Input</label>
<!--end::Label-->

<!--begin::Select2-->
<select class="form-select" name="select2_input" data-control="select2" data-placeholder="Select an option">
<option></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<!--begin::Select2-->
</div>
<!--end::Input group--->

<!--begin::Actions-->
<button id="kt_docs_formvalidation_select2_submit" type="submit" class="btn btn-primary">
<span class="indicator-label">
Validation Form
</span>
<span class="indicator-progress">
Please wait... <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
</span>
</button>
<!--end::Actions-->
</form>
<!--end::Form-->

Tagify

For more information on Tagify, please visit the official website.
// Define form element
const form = document.getElementById('kt_docs_formvalidation_tagify');

// Init tagify --- for more info, please visit: https://yaireo.github.io/tagify/
var tags = new Tagify(document.querySelector("#kt_tagify"), {
whitelist: ["Tag 1", "Tag 2", "Tag 3", "Tag 4", "Tag 5", "Tag 6", "Tag 7", "Tag 8", "Tag 9", "Tag 10", "Tag 11", "Tag 12"],
maxTags: 6,
dropdown: {
maxItems: 20,           // <- mixumum allowed rendered suggestions
classname: "tagify__inline__suggestions", // <- custom classname for this dropdown, so it could be targeted
enabled: 0,             // <- show suggestions on focus
closeOnSelect: false    // <- do not hide the suggestions dropdown once an item has been selected
}
});

tags.on("change", function(){
// Revalidate the field when an option is chosen
validator.revalidateField('tagify_input');
});

// Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
var validator = FormValidation.formValidation(
form,
{
fields: {
'tagify_input': {
    validators: {
        notEmpty: {
            message: 'Tagify input is required'
        }
    }
},
},

plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap: new FormValidation.plugins.Bootstrap5({
    rowSelector: '.fv-row',
    eleInvalidClass: '',
    eleValidClass: ''
})
}
}
);

// Submit button handler
const submitButton = document.getElementById('kt_docs_formvalidation_tagify_submit');
submitButton.addEventListener('click', function (e) {
// Prevent default button action
e.preventDefault();

// Validate form before submit
if (validator) {
validator.validate().then(function (status) {
console.log('validated!');

if (status == 'Valid') {
    // Show loading indication
    submitButton.setAttribute('data-kt-indicator', 'on');

    // Disable button to avoid multiple click
    submitButton.disabled = true;

    // Simulate form submission. For more info check the plugin's official documentation: https://sweetalert2.github.io/
    setTimeout(function () {
        // Remove loading indication
        submitButton.removeAttribute('data-kt-indicator');

        // Enable button
        submitButton.disabled = false;

        // Show popup confirmation
        Swal.fire({
            text: "Form has been successfully submitted!",
            icon: "success",
            buttonsStyling: false,
            confirmButtonText: "Ok, got it!",
            customClass: {
                confirmButton: "btn btn-primary"
            }
        });

        //form.submit(); // Submit form
    }, 2000);
}
});
}
});
<!--begin::Form-->
<form id="kt_docs_formvalidation_tagify" class="form" action="#" autocomplete="off">
<!--begin::Input group-->
<div class="fv-row mb-10">
<!--begin::Label-->
<label class="required fw-semibold fs-6 mb-2">Tagify Input</label>
<!--end::Label-->

<!--begin::Input-->
<input class="form-control" name="tagify_input" value="" id="kt_tagify" />
<!--end::Input-->
</div>
<!--end::Input group-->
<!--begin::Actions-->
<button id="kt_docs_formvalidation_tagify_submit" type="submit" class="btn btn-primary">
<span class="indicator-label">
Validation Form
</span>
<span class="indicator-progress">
Please wait... <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
</span>
</button>
<!--end::Actions-->
</form>
<!--end::Form-->