akukoder/mykad-validator

A library for validating MyKad or MyKid format

1.0.8 2020-08-22 12:58 UTC

This package is auto-updated.

Last update: 2024-11-19 21:59:39 UTC


README

Build Status GitHub tag (latest by date) Packagist PHP Version Support (custom server) Packagist License

About MyKad

The Government Multi-Purpose Smart Card Project (MPSC) or MyKad is part of the Multimedia Super Corridor (MSC Malaysia) initiative.

Introduction

One of the most annoying thing when dealing with user records is when they entered wrong MyKad/MyKid number. This package helps reduce the burden to deal with invalid input by users.

This package will validate MyKad/MyKid number to make sure:

  • Contains numbers only
  • Valid length
  • Valid date of birth
  • Valid state/country code

Note:

Any other unnecessary characters from the input will be removed, including dashes.

Installation

You can install the package via composer:

composer require akukoder/mykad-validator

Usage

use AkuKoder\MyKadValidator\Validator;

$validator = new Validator;

// This will return false
if ($validator->validate('982404-06-5883')) {
    
}

// This will return false
if ($validator->validate('982404-06-83')) {
    
}

// This will return false
if ($validator->validate('980404-00-5335')) {
    
}

// This will return false
if ($validator->validate('9804AA-00-5335')) {
    
}

// This will return true
if ($validator->validate('980404-06-5335')) {
    
}

Get exception on errors

use AkuKoder\MyKadValidator\Validator;

$validator = new Validator;

// This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidDateException
if ($validator->validate('982404-06-5883', true)) {
    
}

// This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidLengthException
if ($validator->validate('982404-06-83', true)) {
    
}

// This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidCodeException
if ($validator->validate('980404-00-5335', true)) {
    
}

// This will throw \AkuKoder\MyKadValidator\Exceptions\InvalidCharacterException
if ($validator->validate('9804AA-00-5335', true)) {
    
}

Testing

composer test

Reference

  1. https://www.jpn.gov.my/en/informasimykad/mykad/

License

The MIT License (MIT).