Last active
November 7, 2024 15:36
-
-
Save geordee/e51d111426de675c0c0f8503c2003047 to your computer and use it in GitHub Desktop.
Validate Emirates ID
This file contains 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
<!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"> </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> |
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.
Thanks for sharing it @geordee! Is it a good idea to use it for my emiratesidstatuscheck blog? Please respond.
Yes. Please go ahead.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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