0

I have the below code and validation to check if the dates being entered are valid. I need to add some simple javascript validation to check whether the 'expiry' date is before the 'review by' date. If expiry date is a date before review date then ok , if not give an error alert box. thanks for any help.

I'm thinking I can compare the two strings and see if one is < than the other but not sure whats the best way to do this.

<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>
topcat3
  • 2,561
  • 6
  • 33
  • 57

2 Answers2

1

You need to convert your data string into Javascript Date object and compare the objects using the normal operators < and >.

dlock
  • 9,447
  • 9
  • 47
  • 67
0

Using your above reply I have adapted the code as follows:

<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;

}

The dates are passed in as strings and put in the mm/dd/yyyy format which is accepted by new Date()

then I do the comparison and works.

topcat3
  • 2,561
  • 6
  • 33
  • 57
  • You should have used regular expressions to parse the date string instead of using flat substring parsing. In your case, if the month for example is 1 number it will cause all your data object to be messed up. – dlock Mar 04 '13 at 18:45
  • thanks but I have other validation that will ensure its in the format dd/mm/yyyy every time. That will suffice you think? – topcat3 Mar 04 '13 at 21:33
  • It will basically suffice. But I think a good programmer doesn't leave any part of his code to be error-prone. So, it would be better if you validate your data on both ends. – dlock Mar 05 '13 at 02:17
  • so you think this is a good link for my solution http://stackoverflow.com/questions/5465375/javascript-date-regex-dd-mm-yyyy . – topcat3 Mar 05 '13 at 09:40