How to Verify Aadhar Card Number without any API?

  9347 views   1 year ago Javascript
Demo Link

In this article, i will share with you how to verify aadhar card number without using any API. if you don't know what the behind the algorithm using for the created aadhar card number and verify then don't worry i will explain to you how it works.

Verhoeff algorithm is using behind the create a unique aadhar card number and also this algorithm use to verify aadhar card number.

here i will use this algorithm in javascript and make one simple aadharcard verify example for you

Example

<!DOCTYPE html>
<html>
<head>
	<title>Aadhar Card Verify</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
	<div class="container">
		<div class="col-md-6" style="margin-top: 50px;">
		  	<div class="form-group">
		    	<label for="exampleInputAadharCard">Aadhar Card No.</label>
		    	<input type="text" class="form-control" id="exampleInputAadharCard" placeholder="Enter Your Aadhar Card No." name="exampleInputAadharCard">
		    	<small id="message" class="form-text text-muted"></small>
		  	</div>
		  	<button type="button" class="btn btn-primary" onclick="verify()">Verify</button>
		</div>
	</div>
	<script type="text/javascript">
	// multiplication table
	const d = [
	    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
	    [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
	    [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
	    [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
	    [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
	    [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
	    [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
	    [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
	    [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
	    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
	]

	// permutation table
	const p = [
	    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
	    [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
	    [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
	    [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
	    [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
	    [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
	    [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
	    [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
	]

	// validates Aadhar number received as string
	function validate(aadharNumber) {
	    let c = 0
	    let invertedArray = aadharNumber.split('').map(Number).reverse()

	    invertedArray.forEach((val, i) => {
	        c = d[c][p[(i % 8)][val]]
	    })

	    return (c === 0)
	}

	function verify() {
		var message = document.getElementById("message");
		var aadharNo = document.getElementById("exampleInputAadharCard").value;
		if(validate(aadharNo)) {
			message.innerHTML = 'Your aadhar card no. valid';
		} else {
			message.innerHTML = 'Your aadhar card no. not valid';
		}
	}
	</script>
</body>
</html>

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]

Related Articles