Skip to content

Instantly share code, notes, and snippets.

@geordee
Last active July 15, 2024 10:49
Show Gist options
  • Save geordee/e51d111426de675c0c0f8503c2003047 to your computer and use it in GitHub Desktop.
Save geordee/e51d111426de675c0c0f8503c2003047 to your computer and use it in GitHub Desktop.
Validate Emirates ID
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Validate Emirates ID</title>
<link rel="stylesheet" href="https://unpkg.com/mvp.css">
<style>
section {
display: flex;
flex-wrap: wrap;
justify-content: var(--justify-important);
}
.mark-success {
background-color: #7abc31;
text-align: center;
}
.mark-failure {
background-color: #eb4d4f;
text-align: center;
}
</style>
</head>
<body>
<main>
<section>
<header>
<h2>Validate Emirates ID</h2>
<p>Validate format, reasonable data quality, and checkdigit.</p>
</header>
<form onSubmit="return false;">
<label for="emirates-id">Emirates ID</label><br />
<input id="emirates-id" name="emirates-id" placeholder="784-XXXX-XXXXXXX-X" onChange="validateEID()" />
<button onClick="validateEID()">Check</button>
<p id="message">&nbsp;</p>
</form>
</section>
</main>
</body>
<script type="text/javascript">
function validateEID() {
var element = document.getElementById("emirates-id");
var message = document.getElementById("message");
message.className = "";
// regex for emirates id with birth dates between 1900 and 2099
var re = new RegExp(/784[-]*\d{4}[-]*\d{7}[-]*\d/);
var eid = element.value;
if (!re.test(eid)) {
console.log("Emirates ID format is incorrect.");
// set error message
message.classList.add("mark-failure")
message.textContent = "Emirates ID format is incorrect.";
return false;
}
// luhn algorithm for checkdigit
var s = 0,
x = true;
var v = eid.replace(/\D/g, "");
var len = v.length;
var checkdigit = v.charAt(len-1);
for (var n = len-2; n >= 0; n--) {
var c = v.charAt(n),
i = parseInt(c, 10);
if (x && (i *= 2) > 9) i -= 9;
s += i;
x = !x;
}
var checksum = (s * 9) % 10;
// validate checkdigit
if (checksum != checkdigit) {
console.log("Emirates ID check digit is incorrect.");
// set error message
message.classList.add("mark-failure")
message.textContent = "Emirates ID check digit is incorrect.";
return false;
}
console.log("Emirates ID is valid.");
// set error message
message.classList.add("mark-success")
message.textContent = "Emirates ID is valid.";
return true;
}
</script>
</html>
@nabukhas
Copy link

Thank you for providing the code.
This algorithim consider the 4 digits after 784 is the birthdate which is not correct as per ICP.
so it will fail on some cases we have faced.

@geordee
Copy link
Author

geordee commented Jun 24, 2023

I understood it as year of birth, and if the year is changed it remains the old one. Have you seen an ID that does 19 or 20 in digits 4 to 5?

I could update the Regex to var re = new RegExp(/784[-]*\d{4}[-]*\d{7}[-]*\d/);.

@nabukhas
Copy link

I didn't meant that there are emirates id with more than 15 digits the problem here is the 4 digits after 784 is not always represnt birthdate.
the below emirates id has 4 digits after 784 not same as birthdate and as per ICP its valid and it returns data.
I will mask some numbers for privacy.
784-2019-0xxxx49-0
but as per ICP data integration the birthdate for above EID is 21-1-2018.

the above eid will fail with algorothim beacuse the last didgit is 0, however the checksum digit expect it to be 9.

@geordee
Copy link
Author

geordee commented Feb 20, 2024

Thank you Naser.

I started getting a few queries on Emirates ID check digit. I have updated this Gist to consider the 4 digits after 784 any group of 4 numbers. However that does not seem to resolve the issue.

The issue I see is that either the check digit may not be using the Luhn algorithm, or Luhn check digit might be calculated on a truncated sequence of numbers.

Also, please note that, I maintain this as a repo at https://github.com/geordee/eid

@nabukhas
Copy link

Thank You Geordee,

We have contacted the authority but there is nothing worth mentioning they just don't provide any valued information to validate the EID,
currently we are considering some sort of exceptions for those EIDs that dose not follow the Luhn algorithm.

I'll let you know if we get anything new.

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