Aadhaar Number Validation Using JavaScript

In this tutorial, I am going to show you how to validate Aadhaar numbers using JavaScript.

There are some conditions that a valid Aadhaar number must satisfy.

First of all, let us see the conditions.

  1. Aadhaar number must have 12 digits as per UIDAI.
  2. Aadhaar is a random number that never starts with a 0 or 1.
  3. Aadhaar number should not contain any alphabet and special characters.
  4. Aadhaar number must have white space after every 4 digits.

So, here we are going to see how to validate an aadhaar number using regular expressions in JavaScript.

JavaScript

<script> function validateAadhaar(){ 
var regexp = /^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/;
var ano = document.getElementById("aadhharno").value;

if(regexp.test(ano)) {
console.log("Valid Aadhaar Number");
return true;
}else{
console.log("Invalid Aadhaar Number");
return false;
} }
</script>

HTML

<input type="text" name="aadhhar" id="aadhharno" onfocusout="validateAadhaar()">

Here regular expression used /^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/

Let’s understand it in details.

  • / this sign is used to enclose the pattern.
  • ^ represents the starting of the string.
  • [2-9]{1} represents the first number can be between 2-9.
  • [0-9]{3} represents the next 3 numbers after the first number should be any digit between 0-9.
  • \s represents white space.
  • [0-9]{4} represents the next 4 numbers should be between 0-9.
  • \s represents white space.
  • [0-9]{4} represents the next 4 numbers should be between 0-9.
  • $ represents the ending of the string.

Hope you have learned how to validate Aadhaar card number using JavaScript. If you have any query please ask in the comment section.

Leave a Comment