0

Have a good date. I tried create form in frontend magento2.

<form data-hasrequired="* Required Fields" method="post" id="contact-form" enctype="multipart/form-data" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}'>
 ...
     <input type="date" value="04-07-2016" class="type-date" max="19-07-2016" min="19-06-2016" id="okes" name="date-854" aria-invalid="true">
  ...
</form>
    <script>
     require(["jquery", "jquery/ui", "mage/calendar", "mage/validation"],
     function($){
        $("input[type=date]").each(function(){
           var min = $(this).attr('min'),
               max = $(this).attr('max'), minDate = maxDate = "";
               if(typeof min !== typeof undefined && min !== false ){
                 minDate = new Date(min.replace( /(\d{2})[-/](\d{2})[-/](\d+)/, "$2,$1,$3") );
               }
               if(typeof max !== typeof undefined && max !== false ){
                 maxDate = new Date(max.replace( /(\d{2})[-/](\d{2})[-/](\d+)/, "$2,$1,$3") );
               }
               //alert(minDate);
           $(this).calendar({
               showsTime: false,
                buttonText:"<?php echo __('Select Date') ?>",
                minDate: minDate,
                maxDate: maxDate,
                dateFormat: 'mm-dd-yy'
             });
        });
    });
    </script>

when i submit form error in input type=date. It not show error. Please tell me this code what's wrong? Many thank.

this image enter image description here

  • are you want to check valid date? – Jaimin Parikh Jul 19 '16 at 9:20
  • yes i want check validate use to "mage/calendar", "mage/validation" in magento2. – xanka Jul 19 '16 at 9:23
1

add "validate-date" class in your input field to validate date.

You can also use "validate-date-range" class for check max date.

  • thank you answer my question? I tried it but not success. – xanka Jul 19 '16 at 9:57
  • try with apply type="text" – Jaimin Parikh Jul 19 '16 at 10:05
  • if i set type text. code in <script> tag not use to be. – xanka Jul 19 '16 at 10:15
  • you can apply any perticular class and use $(".any_class") in <script> tag. – Jaimin Parikh Jul 19 '16 at 10:53
  • hi. I tried it but it not work. :( – xanka Jul 20 '16 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.