Aadhaar Card Number Validation Using Javascript

Here we will learn how to Validate Aadhaar Card Number in Javascript.

To validate Aadhaar Card Number using javascript we will use Regular Expression and we will see how to use regular expression for it.

We Know that Aadhaar Card has 12-digits Numbers.

We will use Regular Expression according to this.

REGULAR EXPRESSION

Regular Expression would be : /^[2-9]{1}[0-9]{3}\s{1}[0-9]{4}\s{1}[0-9]{4}$/

‘/ ‘ this sign is used to enclose the pattern.

‘^’  represents the starting of the pattern.

‘[2-9]{1}’  represents that the first number can be between [2-9].

After the first number, the remaining 3 digits can be between [0-9].

‘\s’ represents the space in the regular expression.

now like the previous first pattern, the next three patterns should be the same.

example like: 78XX 45XX 97XX

Note: Aadhaar no. is starts with any number except 0 and 1.

Javascript code to match pattern and display result: aadhar card validation

  
<script> 
   function validation()
       {   
            
            var regexp=/^[2-9]{1}[0-9]{3}\s{1}[0-9]{4}\s{1}[0-9]{4}$/;
           
            var x=document.getElementById("aadhaar").value;
           if(regexp.test(x))
               {
                   window.alert("Valid Aadhar no.");
                   
               }
        else{ window.alert("Invalid Aadhar no.");
              }
   </script>

In the above javascript code

1. ” function validation() ” is a function that checks the number.

2. Here ” regexp” is the variable.

3. In ” document.getElementById(“aadhaar”).value ” we are taking entered aadhaar number  from user.

(here aadhaar is Id of an input element)

4.” (regexp.test(x)) “in this condition, we are checking the entered number with the regular expression.

Here ‘test’ is an inbuilt method in JavaScript to match the defined pattern with input.

After checking, it returns true if a match found or returns false if a match not found.

5. ” window.alert(“Valid Aadhar no.”);” if the condition is true then it displays the message in the alert box.

6. “window.alert(“Invalid Aadhar no.”);” if the condition is false then it displays the message in the alert box.

Also reaD: Validating phone numbers in JavaScript

Leave a Reply