检查html日期是否>现在

时间:2016-04-14 03:14:20

标签: javascript html

如果日期超过今天,我如何查看日期? 如果日期大于今天,我希望不提交?

<html>

<head>


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function checkDate() {

            var EnteredDate = $("#since").val(); // For JQuery

            var month = EnteredDate.substring(0, 2);

            var date = EnteredDate.substring(3, 5);

            var year = EnteredDate.substring(6, 10);

            var myDate = new Date(year, date-1, month);

            var today = new Date();

            if (myDate > today) {
                alert("Entered date is greater than today's date ");
            }

        }
</script>

</head>

<body>      


        <form  action="">


            <ul>

                <li>
                    <label>Member Since </label>

            <input id = "since" name="since" size="2" maxlength="6" value="" type="date" required/>

                </li>

                <li>

                    <input type="Submit" name="Submit" id="btsubmit" value="Submit" onclick="checkDate();"/>
                </li>
            </ul>
        </form>




</body>

</html>
  

更新

尝试@Gaurav Mahindra回答EnteredDate.substring(X,X)中的微小变化; 因为html的input type="date"属性格式是mm-dd-yyyy 它为什么不工作呢?

1 个答案:

答案 0 :(得分:0)

这对我有用。希望它能帮到你: -

<html>
    <head>
        <title>Date Check</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            function checkDate() {
            //var EnteredDate = document.getElementById("txtdate").value; //for javascript

            var EnteredDate = $("#txtdate").val(); // For JQuery

            var date = EnteredDate.substring(0, 2);
            var month = EnteredDate.substring(3, 5);
            var year = EnteredDate.substring(6, 10);

            var myDate = new Date(year, month-1, date);

            var today = new Date();

            if (myDate > today) {
                alert("Entered date is greater than today's date ");
            }

        }
        </script>
    </head>

    <body>
        Enter Date in (dd-mm-yyyy format) : <br/>
        <input type="text" id="txtdate" name="txtdate"/>
        <input value="SUBMIT"  type="submit" id="btnsubmit" onclick="checkDate();"/>
    </body>

</html>