How to configure jquery.maskedinput to accept only valid dates?

0

How do I <input type="text" name="calendar" id="calendar"> accept only correct dates, example of a correct date 01/02/2017 but it is accepting when you type 01/20/2017, ie month 20 does not exist, type of <input> must continue with "text", follow the example with the problem.

$(document).ready(function(){
  $('#calendar').mask('99/99/9999');
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
<input type="text" id="calendar" name="calendar">
    
asked by anonymous 06.02.2017 / 19:55

2 answers

1

I believe it to be this:

$(document).ready(function(){
  $('#calendar').mask('99/99/9999');
  });

function validateData(){

    var dob = document.getElementById("calendar").value;
    var data = dob.split("/");

    if (isNaN(Date.parse(data[2] + "-" + data[1] + "-" + data[0]))) {
        document.getElementById("calendar").value = "";
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
<input type="text" onBlur="validateData()" id="calendar" name="calendar">
    
06.02.2017 / 20:03
2

You can try using the HTML5 pattern.

<input type="text" pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" required />

I do not know if the regular expression is correct. I removed the information from this post link

    
06.02.2017 / 20:15