Last active
February 4, 2022 09:46
-
-
Save niraj-shah/b7fef71ff0956eefe5d27a28ddbdec21 to your computer and use it in GitHub Desktop.
PHP Regex to validate Emirates ID number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Regex example to validate the format of a Emirates | |
* ID number. Does not validate the checkbit (Luhn Algorithm). | |
* | |
* @author Niraj Shah <[email protected]> | |
*/ | |
// 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>"; | |
} | |
} |
@niraj-shah: Helpful. many thanks.
Excuse me, how do you know it uses Luhn Algorithm? I cannot found formal reference mentioned about that.
can someone help with this?
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
Excuse me, how do you know it uses Luhn Algorithm? I cannot found formal reference mentioned about that.