Hi All

I am hoping somebody can help me with this. I have two drop down
select boxes (Month, Year) which I use to select a credit card expiry
date. I want to check if the month and year they select is before the
current month and year (to check if credit card has expired). I have
created a custom validation method to do this using the jquery
validation plugin. The code for the addMethod is below:

// add method to validate expiry date
    $.validator.addMethod("expirydate", function(value, element) {
         var result = true;
         var strYear = value;
         var strMonth = $("#fMonth option:selected").val();
         var datNow = new Date();
         var intMonth = datNow.getMonth()+1;
         var intYear = datNow.getFullYear()
         if(strYear.length==2) {
              intYear = "" + intYear
              strYear = intYear.substring(0,2) + strYear
         }
         alert(strYear);
         if (parseInt(strYear) < parseInt(intYear)) { result =
false; }
         if (parseInt(strMonth) < parseInt(intMonth)) { result =
false; }
         // return result
         return this.optional(element) || result==true;
    }, "Card has already expired");

Problem is that my jquery does work at all on the page when I add this
method. The problem is with the following two lines:

 if (parseInt(strYear) < parseInt(intYear)) { result = false; }
 if (parseInt(strMonth) < parseInt(intMonth)) { result = false; }

I cant see anything wrong with this code and cant understand why it
does not work. Here is the html code for the select boxes

<tr>
    <td class="formLabel"><span class="required">*</span>Expiry Date</
td>
    <td class="formField"><select name="fMonth" id="fMonth">
    <option selected="selected" value="">Month</option>
    <option value="01">01</option>
    <option value="02">02</option>
    <option value="03">03</option>
    <option value="04">04</option>
    <option value="05">05</option>
    <option value="06">06</option>
    <option value="07">07</option>
    <option value="08">08</option>
    <option value="09">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    </select>
    <select name="fYear" id="fYear">
    <option selected="selected" value="">Year</option>
    <option value="09">2009</option>
    <option value="10">2010</option>
    <option value="11">2011</option>
    </select>
    </td>
  </tr>

The following code is how I implemented the validation:

rules: {
            fYear: {
                required: function() {
                    return $("input[name=rbPaymentType]")
[0].checked==true;
                },
                expirydate
            }
}

If anybody can help I would greatly appreciate it.

Thanks
mwskuzzy


Reply via email to