1. Go to this page and download the library: Download teknyo/lk-nic-validator-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
teknyo / lk-nic-validator-php example snippets
use LK\NICValidator\NICValidator;
use LK\NICValidator\ValidationOptions;
// Simple format validation (no logical checks)
NICValidator::isSimpleValidNIC('853456789V'); // true
NICValidator::isSimpleValidNIC('123'); // false
// Full validation with logical checks
$result = NICValidator::isFullValidNIC('198534567890');
var_dump($result->isValid); // true
var_dump($result->errorReason); // null
// Standard validation
$validation = NICValidator::validateNIC('853456789V');
var_dump($validation->isValid); // true
var_dump($validation->format); // 'old'
var_dump($validation->gender); // 'male' or 'female'
var_dump($validation->birthYear); // 1985
use LK\NICValidator\NICValidator;
$details = NICValidator::getNICDetails('853456789V');
if ($details->isValid) {
echo $details->gender; // 'male' or 'female'
echo $details->birthYear; // 1985
echo $details->birthMonth; // 1-12
echo $details->birthDay; // 1-31
echo $details->dayOfYear; // 1-366
echo $details->age; // Age in years
echo $details->normalizedNIC; // '1985345678' (new format)
echo $details->birthDate->format('Y-m-d'); // Date object
} else {
echo $details->errorReason;
}
use LK\NICValidator\NICValidator;
use LK\NICValidator\ValidationOptions;
// Check if person would be over a certain age
$options = new ValidationOptions(
checkExpiry: true,
maxAge: 120
);
$result = NICValidator::validateNIC('190001154567', $options);
if (!$result->isValid) {
echo $result->error; // "Person would be over 120 years old"
}
type NICFormat = 'old'|'new'|'invalid';
type Gender = 'male'|'female'|null;
class NICResult {
public readonly bool $isValid;
public readonly ?NICFormat $format;
public readonly ?int $birthYear;
public readonly ?int $birthMonth;
public readonly ?int $birthDay;
public readonly Gender $gender;
public readonly ?string $error;
}
class NICDetails {
public readonly bool $isValid;
public readonly string $nicNumber;
public readonly ?string $normalizedNIC;
public readonly NICFormat $format;
public readonly ?string $errorReason;
public readonly ?\DateTime $birthDate;
public readonly ?int $birthYear;
public readonly ?int $birthMonth;
public readonly ?int $birthDay;
public readonly ?int $dayOfYear;
public readonly Gender $gender;
public readonly ?int $age;
}
class ValidationResult {
public readonly bool $isValid;
public readonly ?string $errorReason;
}
class ValidationOptions {
public readonly bool $checkExpiry;
public readonly int $maxAge;
public function __construct(
bool $checkExpiry = false,
int $maxAge = 120
) {}
}
use LK\NICValidator\NICValidator;
// In a Form Request
public function rules(): array
{
return [
'nic_number' => ['('nic_number');
if (!NICValidator::isFullValidNIC($nic)->isValid) {
$validator->errors()->add(
'nic_number',
'The NIC number is invalid.'
);
}
});
}
use LK\NICValidator\NICValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class NICValidatorConstraint extends Constraint
{
public $message = 'The NIC number "{{ string }}" is not valid.';
}
class NICValidatorConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!NICValidator::isFullValidNIC($value)->isValid) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
bash
cd packages/php
composer install
composer test
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.