Whether the date is legal, you need to understand that leap year and ordinary year February days are not the same, there is a large month and small month days are not the same, with a simple statement or can be achieved, but now the requirement to determine whether the date is legal package a function, parameters are respectively year, month, day. This is a bit difficult, the code is as follows, I did not use the FUNCTION wrapped separately in JS file, easier to observe.

HTML: fiefldSet form field set, add a nice interface, look like this

<fieldset>
        <legend>Is the date valid?</legend>
        <span>Years:</span><input type="text" id="inp1"> <span> </span><input type="text" id="inp3"> <button ID =" BTN ">Copy the code

 <script>
        // Wrap the document.getelementByid () function with a simplified get function
        function get(id) {
            return document.getElementById(id);
        }
        // Encapsulate the leap year. N is the year
        function o_year(n) {
            if (n % 4= =0 && n % 100! =0 || n % 400= =0) {
                return true;
            } else return false;
        }
        // Encapsulate the judgment date with year, month, and day
        function o_date(year, month, day) {
            // Check that the year cannot be empty and cannot be a decimal
            if(year ! ="" && year % 1= =0) {
                // Check whether the month is valid
                if (month <= 12 && month >= 1 && month % 1= =0) {
                    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                        // If the month is 31 days, return false
                        if (day <= 31 && day >= 1 && day % 1= =0) {
                            return true;

                        } else return false;
                    }
                    if (month == 4 || month == 6 || month == 9 || month == 11) {
                        // Check whether the month is 30 days
                        if (day <= 30 && day >= 1 && day % 1= =0) {
                            return true;

                        } else return false;
                    }
                    if (month == 2) {
                        // Call the leap year function to determine whether year is a leap year
                        if (o_year(year) == true) {
                            // If February is 29 days, return false
                            if (day >= 1 && day <= 29 && day % 1= =0) {
                                return true;
                            } else return false
                        } else {
                            // If February is 28 days, return false
                            if (day >= 1 && day <= 28 && day % 1= =0) {
                                return true
                            } else return false}}return true;
                } else return false;
            } else return false
        }

        var oInp1 = get('inp1')
        var oInp2 = get('inp2')
        var oInp3 = get('inp3')
        var oBtn = get('btn')
        oBtn.onclick = function() {
            if (o_date(inp1.value, inp2.value, inp3.value) == true) {
                alert("Date valid")}else alert("The date is not valid")
        }
    </script>
Copy the code

The result: Leap years have more than 29 February days, so the date is illegal.