检查javascript到期日期是在审核日期之前

时间:2013-03-04 12:31:41

标签: javascript validation

我有以下代码和验证,以检查输入的日期是否有效。我需要添加一些简单的javascript验证来检查“到期”日期是否在“审核日期”之前。如果到期日期是审核日期之前的日期那么确定,如果没有给出错误警告框。谢谢你的帮助。

我想我可以比较两个字符串,看看是否有<比另一个但不确定最好的方法是什么。

<tr>
    <td class="SubHeader" height="5%">Review</td>
</tr>
<tr>
<td class="Label">Review By</td>
<td>
    <input class="amdInputText" type="text" id="guaranteereviewbydate" value="">
    <xsl:attribute name="value"><xsl:value-of select="guaranteereviewbydate"/></xsl:attribute>
    </input>
</td>
</tr>
<tr>
<td class="Label">Expire On</td>
<td><input class="amdInputText" type="text" id="guaranteeexpireondate" value="">
    <xsl:attribute name="value"><xsl:value-of select="guaranteeexpireondate"/></xsl:attribute>
    </input>
</td>
</tr>

<xsl:if test="count(../bankguaranteedata) &gt; '1'">
else if(!validateDate(document.lending.guaranteereviewbydate[<xsl:value-of select="@id"/>].value)){alert("Please enter a valid review by date. The date must be of the format dd/mm/yyyy");document.lending.guaranteereviewbydate[<xsl:value-of select="@id"/>].focus();return false;}                                  
else if(!validateDateExpireOn(document.lending.guaranteeexpireondate[<xsl:value-of select="@id"/>].value)){alert("Please enter a valid expire on date. The date must be of the format dd/mm/yyyy");document.lending.guaranteeexpireondate[<xsl:value-of select="@id"/>].focus();return false;}
</xsl:if>


<xsl:if test="count(../bankguaranteedata) = '1'">
else if(!validateDate(document.lending.guaranteereviewbydate.value)){alert("Please enter a valid review by date. The date must be of the format dd/mm/yyyy");document.lending.guaranteereviewbydate.focus();return false;}                                  
else if(!validateDateExpireOn(document.lending.guaranteeexpireondate.value)){alert("Please enter a valid expire on date. The date must be of the format dd/mm/yyyy");document.lending.guaranteeexpireondate.focus();return false;}
</xsl:if>

2 个答案:

答案 0 :(得分:1)

您需要将数据字符串转换为Javascript Date object并使用普通运算符<>比较对象。

答案 1 :(得分:0)

使用您的上述回复我已按如下方式修改了代码:

<xsl:if test="count(../documentarycreditdata) &gt; '1'">                
else if(!compareDates(document.lending.documentaryexpireondate[<xsl:value-of select="@id"/>].value, document.lending.documentaryreviewbydate[<xsl:value-of select="@id"/>].value)){alert("Please enter an expire on date which is later than the review by date");document.lending.documentaryexpireondate[<xsl:value-of select="@id"/>].focus();return false;}
</xsl:if><xsl:if test="count(../documentarycreditdata) = '1'">          
else if(!compareDates(document.lending.documentaryexpireondate.value, document.lending.documentaryreviewbydate.value)){alert("Please enter an expire on date which is later than the review by date");document.lending.documentaryexpireondate.focus();return false;}
</xsl:if>

 function compareDates(expirydate, reviewbydate){
 if(expirydate.length <11 && reviewbydate.length <11)
    {
try{
  dayexpire = expirydate.substring(0,2);
  monthexpire = expirydate.substring(3,5);
  yearexpire = expirydate.substring(6,10);
  dayreview = reviewbydate.substring(0,2);
  monthreview = reviewbydate.substring(3,5);
  yearreview = reviewbydate.substring(6,10);
  totalexpire = expirydate.substring(3,5)+"/"+expirydate.substring(0,2)+"/"+expirydate.substring(6,10);
  totalreview = reviewbydate.substring(3,5)+"/"+reviewbydate.substring(0,2)+"/"+reviewbydate.substring(6,10);
  newexpirydate = new Date(totalexpire);
  newreviewdate = new Date(totalreview);
  if(newexpirydate < newreviewdate)
     {
       return false;
     }
  else
     return true;
}catch(e){return false;}
 }
 else
 return false;

}

日期以字符串形式传递,并以mm / dd / yyyy格式传入,新日期()接受

然后我进行比较并运作。