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:
- <form id="mainForm">
- <table>
- <tr>
- <td>
- <select id="drlIdType">
- <option value="IC">IdentityCard</option>
- <option value="PP">Passport</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>DOB(DD/MM/YYYY):</td>
- <td><input type="text" id="txtDOB" class="datepicker" name="txtDOB"></input></td>
- </tr>
- <tr>
- <td>Issued Date:</td>
- <td><input type="text" id="txtIdIssueDate" class="datepicker" name="txtIdIssueDate"></input></td>
- </tr>
- </table>
- </form>
Here my js code:
- <script type="text/javascript">
- $(document).ready(function(){
- $('.datepicker').datepicker();
-
- //Validate email and phone number
- $("#mainForm").validate({
- messages:
- {
- txtDOB:
- {
- checkDateOfBirth: "Error1!"
- },
- txtIdIssueDate:
- {
- checkIdentityCard: "Error2!"
- }
- },
- invalidHandler: function(event, validator) {
- event.preventDefault();
- event.stopPropagation();
- }
- });
-
- /**
- * Check date of birth
- * @return: true if (now - date of birth) >= 18
- */
- $.validator.addMethod("checkDateOfBirth", function(value, element){
- var inputDate = $('#txtDOB').val();
- var currdate = new Date();
- var yearInput = 18;
- return compareDate(inputDate, currdate, yearInput, '>=');
- });
-
- /**
- * Check identity card if idtype = IC
- * @return: true if (issuedt date - date of birth) >= 14
- */
- $.validator.addMethod("checkIdentityCard", function(value, element){
- if ($("#drlIdType").val() == 'IC')
- {
- frDate = $("#txtDOB").val();
- var toDate = $("#txtIdIssueDate").val();
- var yearInput = 14;
- return compareDateStr(frDate, toDate, yearInput, '>=');
- }
- else
- {
- return true;
- }
- });
-
- var test = {
- txtCheckDateOfBirth: {
- checkDateOfBirth: true
- },
- txtIdIssueDate: {
- checkIdentityCard: true
- }
- }
-
- $("#txtDOB").rules('add', test['txtCheckDateOfBirth']);
- $("#txtIdIssueDate").rules('add', test['txtIdIssueDate']);
- $(".datepicker").datepicker('option', {
- onSelect: function(dateText, inst){
- $(this).valid();
- }
- });
- });
- /**
- * Comparing two date fullyear
- * @param {String} frDtInput : from date input string with format: dd/mm/yyyy
- * @param {String} toDtInput: to date input string with format: dd/mm/yyyy
- * @param {Number} yearInput : input number to be compared
- * @param {String} comparision operator
- * @return {Boolean} True if (toDtInput - frDtInput) > 0 else False
- */
- function compareDateStr(frDtInput, toDtInput, yearInput, compareOper)
- {
- if (frDtInput && toDtInput && yearInput)
- {
- var arrDate = "";
- arrDate = frDtInput.split("/");
- var day = arrDate[0];
- var month = arrDate[1];
- var year = arrDate[2];
- var frDate = new Date();
- frDate.setFullYear(year, month-1, day);
- arrDate = toDtInput.split("/");
- day = arrDate[0];
- month = arrDate[1];
- year = arrDate[2];
- var toDate = new Date();
- toDate.setFullYear(year, month, day);
- toDate.setFullYear(toDate.getFullYear() - yearInput);
- if (compareOper == '>='){
- return (toDate - frDate >= 0 ? true : false);
- } else if (compareOper == '<=') {
- return (toDate - frDate <= 0 ? true : false);
- } else if (compareOper == '>') {
- return (toDate - frDate > 0 ? true : false);
- } else if (compareOper == '<') {
- return (toDate - frDate < 0 ? true : false);
- } else if (compareOper == '=') {
- return (toDate - frDate <= 0 ? true : false);
- } else {
- return false;
- }
- }
-
- }
- /**
- * Comparing two date fullyear
- * @param {String} frDtInput : from date input string with format: dd/mm/yyyy
- * @param {Date} toDtInput: to date input
- * @param {Number} yearInput : input number to be compared
- * @param {String} comparision operator
- * @return {Boolean} True if (toDtInput - frDtInput) > 0 else False
- */
- function compareDate(frDtInput, toDtInput, yearInput, compareOper)
- {
- if (frDtInput && toDtInput && yearInput)
- {
- var arrDate = "";
- arrDate = frDtInput.split("/");
- var day = arrDate[0];
- var month = arrDate[1];
- var year = arrDate[2];
- var frDate = new Date();
- frDate.setFullYear(year, month-1, day);
- var toDate = new Date();
- toDate = toDtInput.setFullYear(toDtInput.getFullYear() - yearInput);
- if (compareOper == '>='){
- return (toDate - frDate >= 0 ? true : false);
- } else if (compareOper == '<=') {
- return (toDate - frDate <= 0 ? true : false);
- } else if (compareOper == '>') {
- return (toDate - frDate > 0 ? true : false);
- } else if (compareOper == '<') {
- return (toDate - frDate < 0 ? true : false);
- } else if (compareOper == '=') {
- return (toDate - frDate <= 0 ? true : false);
- } else {
- return false;
- }
- }
-
- }
- </script>