我有一个JSP,其中有一个表单。此表单中的一个字段是文本框,我接受日期。我有一个日历,用户可以从中选出日期。现在,当用户选择以“dd-mm-yyyy”格式显示在文本框中的日期时。 (例如:2012年6月11日)
表格一提交,就会变更为长(例如:2012年1月11日星期一00:00:00) 现在,如果添加了更多搜索条件,则会因为未验证日期而引发错误。
如何防止这种情况发生?
以下代码:
<script type="text/javascript">
function formDateValidate() {
var flag = 0;
var message = "";
if (document.searchTemporaryPromotion.tempPromotionFromDate.value != ""
|| document.searchTemporaryPromotion.tempPromotionToDate.value != ""
|| document.searchTemporaryPromotion.softwarePartNumber.value != "") {
document.searchTemporaryPromotion.resolve.value = "";
}
printMessageForDiv(new Array("dateLabel"), new Array(""));
if (document.searchTemporaryPromotion.tempPromotionFromDate.value != "") {
var result1 = validateDate(document.searchTemporaryPromotion.tempPromotionFromDate);
if (result1 != "") {
message = message
+ "Check From Date: "
+ result1
+ " You entered: "
+ document.searchTemporaryPromotion.tempPromotionFromDate.value
+ ".<br>";
printMessageForDiv(
new Array("dateLabel", "fromDateLabelField"),
new Array(
message,
"<font color='red'><b>"
+ '*<spring:message code="label.temporaryPromotion.fromDate" />:*'
+ "</b></font>"));
++flag;
}
}
if (document.searchTemporaryPromotion.tempPromotionToDate.value != "") {
var result2 = validateDate(document.searchTemporaryPromotion.tempPromotionToDate);
if (result2 != "") {
message = message
+ "Check To Date: "
+ result2
+ " You entered: "
+ document.searchTemporaryPromotion.tempPromotionToDate.value
+ ".<br>";
printMessageForDiv(
new Array("dateLabel", "toDateLabelField"),
new Array(
message,
"<font color='red'><b>"
+ '*<spring:message code="label.temporaryPromotion.toDate" />:*'
+ "</b></font>"));
++flag;
}
}
if (flag != 0) {
document.getElementById("entireViewForSearch").style.visibility = 'hidden';
return false;
} else {
return true;
}
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>temporaryPromotion</title>
</head>
<body>
<span id="dateLabel" class="errordiv"></span>
<span id="muidFieldLabel" class="errordiv"> </span>
<form:form id="searchTemporaryPromotion"
name="searchTemporaryPromotion"
commandName="searchTemporaryPromotionCommand" onsubmit="return checkFormOnSearch(this);">
<form:hidden path="currentPage" />
<form:hidden path="usePaging" />
<form:hidden path="itemsPerPage" />
<form:hidden path="action" />
<div id="dialog" style="display: none"></div>
<p>
<i><font color="red">*Note: Date format should be
dd-MM-yyyy.</font> </i>
</p>
<spring:hasBindErrors name="searchTemporaryPromotionCommand">
<p>
<b><form:errors cssStyle="color: red;"></form:errors> </b>
</p>
</spring:hasBindErrors>
<table>
<tr>
<th colspan="2" style="text-align: left; padding-left: 5px">Search
Criteria</th>
</tr>
<tr>
<td><span id="fromDateLabelField"><spring:message
code="label.reporting.searchTemporaryPromotion.fromDate" />:</span> <form:input
path="tempPromotionFromDate" id="tempPromotionFromDate"
cssStyle="width: 70px;text-align: center" /> <input type="button"
value="..."
onclick="displayCalendar(document.getElementById('tempPromotionFromDate'),'dd-mm-yyyy',this);">
<span id="toDateLabelField"> <spring:message
code="label.reporting.searchTemporaryPromotion.toDate" />:</span> <form:input
path="tempPromotionToDate" id="tempPromotionToDate"
cssStyle="width: 70px;text-align: center" /> <input type="button"
value="..."
onclick="displayCalendar(document.getElementById('tempPromotionToDate'),'dd-mm-yyyy',this);">
</td>
</tr>
<tr>
<td><spring:message
code="label.reporting.searchTemporaryPromotion.partNumber" />: <form:input
path="softwarePartNumber" id="softwarePartNumber"
name="softwarePartNumber" cssStyle="width: 215px;" /></td>
</tr>
<tr>
<td><input type="submit" value="Search" onClick="javascript:onSearch();" onKeyPress="javascript:onSearch();"/>
<input type="button" value="Clear" onClick="javascript:onClear(this);" /></td>
</tr>
</table>
</form:form>
答案 0 :(得分:1)
我找不到你的validateDate()函数,但我怀疑它会改变函数中生成的JS Date Object输入的原始值。
尝试在不改变输入值的情况下进行验证。