Magento – How to add validation for date in Magento 2

checkoutjavascriptmagento2validation

I have this input and i want to add validation for date

  <input class="input-text validate-date" data-validate="{required:true}"
    value: value,
    valueUpdate: 'keyup',
    hasFocus: focused,
    attr: {
        name: inputName,
        placeholder: placeholder,
        'aria-describedby': noticeId,
        id: uid,
        disabled: disabled
    }" name="custom_attributes[amorderattr_birth__date]" placeholder="" aria-describedby="notice-N780KER" id="N780KER" style="

I already added this class class="input-text validate-date" data-validate="{required:true}"> but nothing happen.

Does anyone have any idea how it's the proper way to add date validation for this filed?

Best Answer

put this code into your phtml

<script>
   require([
   "jquery",
   "mage/mage",
   "mage/validation"
  ], function($){
        $(document).ready(function () {
            var dataForm = $('#<form_id>');                 
            var ignore = null;

            dataForm.mage('validation', {
                    ignore: ignore ? ':hidden:not(' + ignore + ')' : ':hidden'
                }).find('input:text').attr('autocomplete', 'off');
            if (dataForm.validation('isValid') == false) {
                return false;
            }
     });
</script>
Related Topic