下面的代码用于在单击按钮时验证开始和结束日期(两个文本框和日期是从日历中选择的)。代码工作正常。问题是我还有 8 个开始和结束日期要验证。所以由于代码长度,任何人都可以告诉我一个通用代码来验证所有开始和结束日期。
function validated() {
var cal1=document.getElementById('<%=EndDateTwo.ClientId%>').value;
var cal2= document.getElementById('<%=StartDateTwo.ClientId%>').value;
if (cal1 == '' || cal2 == '') {
alert("Start Date and End Date can not bleft blank.");
return false;
}
var dt1 = Date.parse(cal1);
var dt2 = Date.parse(cal2);
if (dt1 <= dt2) {
alert("End Date must occur after the Start date ");
return false;
}
return true;
我正在使用 html 按钮来调用它
<asp:Button ID="Button2" OnClientClick="return validated();" runat="server" Text="Enable" />
最佳答案
您可以创建一个方法 validate(cal1, cal2)
并为您希望在 validate()
方法中验证的每个日期对调用此方法
关于javascript - 如何使用javascript验证日期的多个文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30938369/