Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Last active February 4, 2022 09:46
Show Gist options
  • Save niraj-shah/b7fef71ff0956eefe5d27a28ddbdec21 to your computer and use it in GitHub Desktop.
Save niraj-shah/b7fef71ff0956eefe5d27a28ddbdec21 to your computer and use it in GitHub Desktop.
PHP Regex to validate Emirates ID number
<?php
/**
* Regex example to validate the format of a Emirates
* ID number. Does not validate the checkbit (Luhn Algorithm).
*
* @author Niraj Shah <niraj@webniraj.com>
*/
// regex to validate the format xxx-xxxx-xxxxxxx-x (Emirates ID)
$regex = '/^784-[0-9]{4}-[0-9]{7}-[0-9]{1}$/';
// Emirates IDs to check
$eids = [
"784-1980-1234567-9",
"123-1234-0123456-7",
"784198012345679"
];
// empty array to store matches
$matches = [];
foreach ( $eids as $id ) {
preg_match( $regex, $id, $matches );
// if match, show VALID
if ( count( $matches ) == 1 ) {
echo "{$id}: VALID</br>";
} else {
echo "{$id}: INVALID</br>";
}
}
@anguschiu1
Copy link

Excuse me, how do you know it uses Luhn Algorithm? I cannot found formal reference mentioned about that.

@khushaldusseja
Copy link

@niraj-shah: Helpful. many thanks.

@manisha12ram
Copy link

Excuse me, how do you know it uses Luhn Algorithm? I cannot found formal reference mentioned about that.
can someone help with this?

@niraj-shah
Copy link
Author

Excuse me, how do you know it uses Luhn Algorithm? I cannot found formal reference mentioned about that.

While there is no official documentation, the algorithm has been tested against real EID cards.

It hasn't been tested against thousands of valid EID cards as it's difficult to obtain a database to test against. You can check it against your own and see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment