Hi all,
I want to validate a group fields using datepicker and validation plugin. My problem is when i change one value (textbox), all elements aren't re-validated.
Ex:
I manually select textbox to change value:
-Textbox 1: 5/5/1987 -> (return true, because (now - date of birth) > 18).
-Textbox 2: 5/5/2000 -> Error 2 (return false -> display error 2, 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.
Here my js code:
compareDate and compareDateStr are functions to return boolean after checking inputting conditions.
- $('.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();
- }
- });