繁体   English   中英

在ASP.Net C#中使用JavaScript进行当前日期验证

[英]Current Date Validation using JavaScript in ASP.Net C#

我正在尝试验证日期输入框以检查它是否是当前日期/未来日期/无效日期,我正在使用以下代码,但它根本不起作用。 我尝试使用div标签和标签,但它不会只显示任何东西。 我从stackoverflow本身获取了这段代码。 请帮忙。提前谢谢。

这是我的ASPX代码

                            <div class="form-group">
                                <span class="txt_orange">*</span><span class="LabelClass">Request Created on</span>
                                <br />
                                <asp:TextBox ID="txt_req_createdon_new" runat="server" CssClass="form-control dtPick funkystyling" onBlur="CheckForEmpty(this.id,'Select Start Date')" onkeyup="checkDate()" ></asp:TextBox>
                                <asp:CalendarExtender ID="CalendarExtender3" runat="server" Enabled="True" TargetControlID="txt_req_createdon_new" CssClass="red" Format="dd/MM/yyyy">`enter code here`
                                </asp:CalendarExtender>
                                <%--<asp:Label ID="Valid" runat="server" Text="Label"></asp:Label>--%>

                            </div>

                            <div id="Valid" runat="server"></div> 

这是JavaScript代码

function isFutureDate(idate) {
            var today = new Date().getTime(),
                idate = idate.split("/");

            idate = new Date(idate[2], idate[1] - 1, idate[0]).getTime();
            return (today - idate) < 0 ? true : false;
        }    

        function checkDate() {
            var idate = document.getElementById("txt_req_createdon_new"),
            resultDiv = document.getElementById("Valid"),

            dateReg = /(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/]201[4-9]|20[2-9][0-9]/;               

            if (dateReg.test(idate.value)) {
                if (isFutureDate(idate.value)) {
                    resultDiv.innerHTML = "Entered date is a future date";
                    resultDiv.style.color = "red";
                } else {
                    resultDiv.innerHTML = "It's a valid date";
                    resultDiv.style.color = "green";
                }
            } else {
                resultDiv.innerHTML = "Invalid date!";
                resultDiv.style.color = "red";
            }

     }

将ClientIDMode设置为static。

默认情况下,ClientIDMode为Auto,这意味着,客户端ID将通过将每个父命名容器的ID值与控件的ID值连接来生成。 看到

例:

<asp:Label ID="Valid" ClientIDMode="Static" runat="server" Text="Label"></asp:Label>

所以我很高兴地告诉你,你的脚本运行正常。 我测试了它,结果如下。

在此输入图像描述在此输入图像描述

现在问题是,根据我的想法,您可能已将脚本保留在页面的顶部。 这意味着它在声明TextBox代码之前保存在某处。 尝试将脚本放在页面底部。 body或任何你喜欢的地方,但它应该在结束附近。 这可能有所帮助。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM