验证未来日期的正则表达式

时间:2013-06-18 09:47:22

标签: c# asp.net-mvc-4

我正在使用asp.net开发mvc 4,我想验证出生日期,以便用户无法选择未来的日期,我该怎么做?

<script>
    $(document).ready(function () {
        $("#DOB").datepicker({
            dateFormat: "m/d/yy",
        });
    });
</script>
@Html.TextBox("DOB", Model.DOB.ToString("MM/dd/yyyy"), new { @class = "cssClass" })

我不希望服务器端验证我想在客户端验证。我可以用正则表达式验证它吗?

解决方案:

$(document).ready(function () {
        $("#DOB").datepicker({
            dateFormat: "m/d/yy",
        }).change(function () {
            var date = new Date($("#DOB").val());
            var now = new Date();
            if (date > now) {
                alert("Please enter valid date.");
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

为日,月和年制作3个字段,几乎与任何在线网站一样 用值填充字段。
然后,您可以根据自己的意愿填写年份下拉列表 您只需使用更改事件来验证其他字段即可 另外,您只需使用日+月+年,制作日期值,并与your_date&lt; =新日期()进行比较
否则,您将不得不担心字符串可能具有的所有不同日期格式,这使得无法对其进行验证。

见这里:
Birthday form using HTML/PHP

这样的事情:

<!doctype html>
<html itemscope="itemscope" itemtype="http://schema.org/WebPage">

<head>

    <title>Hello</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>


    <script type="text/javascript">


        function isLeapYear(iYear)
        {
            return (new Date(iYear, 1, 29).getMonth() == 1);
        }

        function getMonthLength(month, year)
        {
            month = month - 1;

            var monthStart = new Date(year, month, 1);
            var monthEnd = new Date(year, month + 1, 1);
            var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);

            return monthLength;
        }


        function PopulateDayDropdown(iMonth, iYear)
        {
            for (i = 1; i < getMonthLength(iMonth, iYear) + 1; i++)
            {
                $('<option/>').val(i).html(i).appendTo('#selDay');
            }


        }

        function PopulateMonthDropdown()
        {

            var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];



            for (i = 0; i < months.length; i++)
            {
                $('<option/>').val(i + 1).html(months[i]).appendTo('#selMonth');
            }

        }


        function PopulateYearDropdown()
        {

            var iCurYear = new Date().getFullYear();

            for (i = iCurYear - 100; i <= iCurYear; i++)
            {
                $('<option/>').val(i).html(i).appendTo('#selYear');
            }

            //$('select option[value="' + iCurYear + '"]').html();
            $('#selYear option[value="' + iCurYear + '"]').attr("selected", "selected")

        }



        $(document).ready(function ()
        {
            // Handler for .ready() called.
            PopulateDayDropdown(1, new Date().getFullYear());
            PopulateMonthDropdown();
            PopulateYearDropdown();



            //$('select[name="month"]').change(function()
            $('select').change(function ()
            {
                //alert("2012 leap year? " + isLeapYear(2012) + "\n" + "2013 leap year? " + );

                var iDay = $("#selDay").val();
                var iMonth = $("#selMonth").val();
                var iYear = $("#selYear").val();

                if (this.id == "selMonth" || this.id == "selYear")
                {
                    $('#selDay').empty();
                    PopulateDayDropdown(iMonth, iYear);
                    $('#selDay option[value="' + iDay + '"]').attr("selected", "selected")
                }


                if (iDay > getMonthLength(iMonth, iYear))
                {
                    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                    alert("There is no " + iDay + "th " + months[iMonth - 1] + ".");

                }



                var d = new Date(iYear, iMonth - 1, iDay, 0, 0, 0, 0);


                if (d > new Date())
                {
                    $('#selDay option[value="' + 1 + '"]').attr("selected", "selected")
                    $('#selMonth option[value="' + 1 + '"]').attr("selected", "selected")
                    alert("Future dates are not allowed (values have been reset to January 1st).");
                    return;
                }


                alert("New date of birth: " + d);

            });


        });

    </script>


</head>


<body>


<form action="registration.php" method="post">

    <p>Select date:</p>


    <select id="selDay" name="day">
    </select>



    <select id="selMonth" name="month">
    <!--
    <option value="January">January</option>
    <option value="February">February</option>
    <option value="Mars">Mars</option>
    <option value="April">April</option>
    <option value="May">May</option>
    <option value="June">June</option>
    <option value="July">July</option>
    <option value="September">September</option>
    <option value="October">October</option>
    <option value="November">November</option>
    <option value="December">December</option>
    -->
    </select>

    <select id="selYear" name="year">

    </select>

    <br/><br/>

    <input type="submit" value="Submit" />

</form>

</body>
</html>