/** * Validate the elements data against the rule * * @param string $data To check * @param int $repeatCounter Repeat group counter * * @return bool true if validation passes, false if fails */ public function validate($data, $repeatCounter) { $errors = array(); $currentTime = JFactory::getDate(); /* DO ID LENGTH TEST */ if (strlen($data) == 13) { /* SPLIT ID INTO SECTIONS */ $year = substr($data, 0, 2); $month = substr($data, 2, 2); $day = substr($data, 4, 2); $gender = substr($data, 6, 4) * 1; $citizen = substr($data, 10, 2) * 1; $check_sum = substr($data, 12, 1) * 1; /* DO YEAR TEST */ $nowYearNotCentury = $currentTime->format('Y'); $nowYearNotCentury = substr($nowYearNotCentury, 2, 2); $year = $year <= $nowYearNotCentury ? '20' . $year : '19' . $year; if (!($year > 1900 && $year < $currentTime->format('Y'))) { $errors[] = FText::_('PLG_VALIDATION_RSA_YEAR_IS_NOT_VALID'); } /* DO MONTH TEST */ if (!($month > 0 && $month < 13)) { $errors[] = FText::_('PLG_VALIDATION_RSA_MONTH_IS_NOT_VALID'); } /* DO DAY TEST */ if (!($day > 0 && $day < 32)) { $errors[] = 'Day is not valid'; } /* DO DATE TEST */ if (($month == 4 || $month == 6 || $month == 9 || $month == 11) && $day == 31) { $errors[] = FText::_('PLG_VALIDATION_RSA_MONTH_NOT_31_DAYS'); } if ($month == 2) { // check for february 29th $isLeap = $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0); if ($day > 29 || $day == 29 && !$isLeap) { $errors[] = FText::sprintf('PLG_VALIDATION_RSA_FEB_CHECK', $day, $year); } } /* DO GENDER TEST */ if (!($gender >= 0 && $gender < 10000)) { $errors[] = FText::sprintf('PLG_VALIDATION_RSA_GENDER_NOT_VALID'); } /* DO CITIZEN TEST */ //08 or 09 SA citizen //18 or 19 Not SA citizen but with residence permit if (!($citizen == 8 || $citizen == 9 || $citizen == 18 || $citizen == 19)) { $errors[] = FText::sprintf('PLG_VALIDATION_RSA_CITIZEN_NOT_VALID'); } /* GET CHECKSUM VALUE */ $checkSumOdd = 0; $checkSumEven = 0; $checkSumEvenTemp = ''; // Get ODD Value for ($count = 0; $count < 11; $count += 2) { $checkSumOdd += substr($data, $count, 1); } // Get EVEN Value for ($count = 0; $count < 12; $count += 2) { $checkSumEvenTemp = $checkSumEvenTemp . substr($data, $count + 1, 1) + ''; } $checkSumEvenTemp = $checkSumEvenTemp * 2; $checkSumEvenTemp = $checkSumEvenTemp + ''; for ($count = 0; $count < strlen($checkSumEvenTemp); $count++) { $checkSumEven += substr($checkSumEvenTemp, $count, 1) * 1; } // GET Checksum Value $checkSumValue = $checkSumOdd * 1 + $checkSumEven * 1; $checkSumValue = $checkSumValue . 'xxx'; $checkSumValue = 10 - substr($checkSumValue, 1, 1) * 1; if ($checkSumValue == 10) { $checkSumValue = 0; } /* DO CHECKSUM TEST */ if ($checkSumValue !== $check_sum) { $errors[] = FText::_('PLG_VALIDATION_RSA_CHECKSUM_NOT_VALID'); } } else { $errors[] = FText::_('PLG_VALIDATION_RSA_ID_NOT_THE_RIGHT_LENGTH'); } if (!empty($errors)) { $this->errorMsg = implode('<br />', $errors); return false; } return true; }