我正在尝试验证日期输入框,以检查它是否是当前日期/未来日期/无效日期,并且我使用以下代码,但它不是工作在一起。我尝试使用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";
}
}
答案 0 :(得分:0)
将ClientIDMode设置为static。
默认情况下,ClientIDMode为Auto,这意味着,将通过将每个父命名容器的ID值与控件的ID值连接来生成客户端ID。 See
示例:
<asp:Label ID="Valid" ClientIDMode="Static" runat="server" Text="Label"></asp:Label>
答案 1 :(得分:0)
所以我很高兴地告诉你,你的脚本运行正常。我测试了它,结果如下。
现在问题是,根据我的想法,您可能已将脚本保留在页面的顶部。这意味着它在声明TextBox
代码之前保存在某处。尝试将脚本放在页面底部。 body
之后或任何你喜欢的地方,但它应该在结尾附近。这可能有所帮助。
希望这有帮助。