Validation Plugin to re-validate group fields using datepicker

Validation Plugin to re-validate group fields using datepicker

Hi all,

I have three elements: 1 drop downlist, 2 textbox using datepicker. Each others have some criteria:
- Textbox 1 (date of birth): if (now - date of birth) < 18 ---> display Error 1.
Textbox 2 (issued date): if ( issued date -  date of birth) < 14 ---> display Error 2.

Whenever one changed, all elements are re-validated to check. Validation is re-validated when i manually select textbox.
Ex:
-Textbox 1: 5/5/1987 -> (return true, because (now - date of birth) > 18).
-Textbox 2: 5/5/2000 -> Error 2 (return false,  because ( issued date -  date of birth ) <14 ).

If i change textbox 1: 5/5/1980, textbox 2 isn't re-validated and still display error2, although (issued date - new date of birth) > 14. 
After that, i want extend to solve this problem with three, four...  concerned  text box with criteria. Anyone help me???

Here my html code:
  1. <form id="mainForm">
  2. <table>
  3. <tr>
  4. <td>
  5. <select id="drlIdType">
  6. <option value="IC">IdentityCard</option>
  7. <option value="PP">Passport</option>
  8. </select>
  9. </td>
  10. </tr>
  11. <tr>
  12. <td>DOB(DD/MM/YYYY):</td>
  13. <td><input type="text" id="txtDOB" class="datepicker" name="txtDOB"></input></td>
  14. </tr>
  15. <tr>
  16. <td>Issued Date:</td>
  17. <td><input type="text" id="txtIdIssueDate" class="datepicker" name="txtIdIssueDate"></input></td>
  18. </tr>
  19. </table>
  20. </form>
Here my js code:
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $('.datepicker').datepicker();
  4. //Validate email and phone number
  5.             $("#mainForm").validate({
  6.                 messages:
  7.                 {
  8.                     txtDOB:
  9.                     {
  10.                         checkDateOfBirth: "Error1!"
  11.                     },
  12.    txtIdIssueDate:
  13.    {
  14. checkIdentityCard: "Error2!"
  15.    }
  16.                 },
  17.                 invalidHandler: function(event, validator) {
  18.                     event.preventDefault();
  19.                     event.stopPropagation();
  20.                 }
  21.             });
  22. /**
  23.             * Check date of birth
  24.             * @return: true if (now - date of birth) >= 18
  25.             */
  26.             $.validator.addMethod("checkDateOfBirth", function(value, element){
  27.                       var inputDate = $('#txtDOB').val();
  28.                       var currdate = new Date();
  29.                       var yearInput = 18;
  30.                       return compareDate(inputDate, currdate, yearInput, '>=');
  31.             });
  32.             /**
  33.             * Check identity card if idtype = IC
  34.             * @return: true if (issuedt date - date of birth) >= 14
  35.             */
  36.             $.validator.addMethod("checkIdentityCard", function(value, element){
  37.                       if ($("#drlIdType").val() == 'IC')
  38.                       {
  39.                           frDate = $("#txtDOB").val();
  40.                           var toDate = $("#txtIdIssueDate").val();
  41.                           var yearInput = 14;
  42.                           return compareDateStr(frDate, toDate, yearInput, '>=');
  43.                       }
  44.                       else
  45.                       {
  46.                         return true;
  47.                       }
  48.             });
  49.             var test = {
  50.                 txtCheckDateOfBirth: {
  51.                     checkDateOfBirth: true
  52.                 },
  53. txtIdIssueDate: {
  54. checkIdentityCard: true
  55. }
  56.             }
  57.             $("#txtDOB").rules('add', test['txtCheckDateOfBirth']);
  58. $("#txtIdIssueDate").rules('add', test['txtIdIssueDate']);
  59. $(".datepicker").datepicker('option', {
  60.                 onSelect: function(dateText, inst){
  61. $(this).valid();
  62.                 }
  63.             });
  64. });

  65. /**
  66.         * Comparing two date fullyear
  67.         * @param {String} frDtInput : from date input string with format: dd/mm/yyyy
  68.         * @param {String} toDtInput: to date input string with format: dd/mm/yyyy
  69.         * @param {Number} yearInput : input number to be compared
  70.         * @param {String} comparision operator
  71.         * @return {Boolean} True if (toDtInput - frDtInput) > 0 else False
  72.         */
  73.         function compareDateStr(frDtInput, toDtInput, yearInput, compareOper)
  74.         {
  75.             if (frDtInput && toDtInput && yearInput)
  76.             {
  77.                 var arrDate = "";
  78.                 arrDate = frDtInput.split("/");
  79.                 var day = arrDate[0];
  80.                 var month = arrDate[1];
  81.                 var year = arrDate[2];

  82.                 var frDate = new Date();
  83.                 frDate.setFullYear(year, month-1, day);

  84.                 arrDate = toDtInput.split("/");
  85.                 day = arrDate[0];
  86.                 month = arrDate[1];
  87.                 year = arrDate[2];

  88.                 var toDate = new Date();
  89.                 toDate.setFullYear(year, month, day);
  90.                 toDate.setFullYear(toDate.getFullYear() - yearInput);

  91.                 if (compareOper == '>='){
  92.                     return (toDate - frDate >= 0 ? true : false);
  93.                 } else if (compareOper == '<=') {
  94.                     return (toDate - frDate <= 0 ? true : false);
  95.                 } else if (compareOper == '>') {
  96.                     return (toDate - frDate > 0 ? true : false);
  97.                 } else if (compareOper == '<') {
  98.                     return (toDate - frDate < 0 ? true : false);
  99.                 } else if (compareOper == '=') {
  100.                     return (toDate - frDate <= 0 ? true : false);
  101.                 } else {
  102.                     return false;
  103.                 }
  104.             }
  105.             
  106.         }

  107. /**
  108.         * Comparing two date fullyear
  109.         * @param {String} frDtInput : from date input string with format: dd/mm/yyyy
  110.         * @param {Date} toDtInput: to date input 
  111.         * @param {Number} yearInput : input number to be compared
  112.         * @param {String} comparision operator
  113.         * @return {Boolean} True if (toDtInput - frDtInput) > 0 else False
  114.         */
  115.         function compareDate(frDtInput, toDtInput, yearInput, compareOper)
  116.         {
  117.             if (frDtInput && toDtInput && yearInput)
  118.             {
  119.                 var arrDate = "";
  120.                 arrDate = frDtInput.split("/");
  121.                 var day = arrDate[0];
  122.                 var month = arrDate[1];
  123.                 var year = arrDate[2];

  124.                 var frDate = new Date();
  125.                 frDate.setFullYear(year, month-1, day);

  126.                 var toDate = new Date();
  127.                 toDate = toDtInput.setFullYear(toDtInput.getFullYear() - yearInput);
  128.                 if (compareOper == '>='){
  129.                     return (toDate - frDate >= 0 ? true : false);
  130.                 } else if (compareOper == '<=') {
  131.                     return (toDate - frDate <= 0 ? true : false);
  132.                 } else if (compareOper == '>') {
  133.                     return (toDate - frDate > 0 ? true : false);
  134.                 } else if (compareOper == '<') {
  135.                     return (toDate - frDate < 0 ? true : false);
  136.                 } else if (compareOper == '=') {
  137.                     return (toDate - frDate <= 0 ? true : false);
  138.                 } else {
  139.                     return false;
  140.                 }
  141.             }
  142.             
  143.         }
  144. </script>