0

我有一个 JSP,其中有一个表单。此表单中的一个字段是我接受日期的文本框。我有一个日历,用户可以从中选择一个日期。现在,当用户选择日期时,它会以“dd-mm-yyyy”格式出现在文本框中。(例如:2012 年 11 月 6 日)

提交表单后,它会立即更改为 long(例如:Mon Jun 11 00:00:00 IST 2012)现在,如果添加了更多搜索条件,则会由于未验证日期而引发错误。

我怎样才能防止这种情况发生?

下面给出的代码:

<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);">
                &nbsp;&nbsp;
                <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>

4

1 回答 1

1

我找不到您的 validateDate() 函数,但我怀疑它会将输入的原始值更改为函数内生成的 JS 日期对象。

尝试在不更改输入值的情况下进行验证。

于 2012-06-28T07:55:05.873 回答