Skip to content

Java RegEx – Validate Aadhar Card Number

Java RegEx – Validate Aadhar Card Number example shows how to validate Aadhar card numbers using Java regular expression pattern.

How to validate the Aadhar card number using regex in Java?

Before we get into how to validate the Aadhar card number using regex, we first need to understand what is it and how the Aadhar card number is generated.

The Aadhar card number is a 12 digit unique identification number and it is issued to individuals by the Unique Identification Authority of India commonly known as UIDAI.

The Aadhar number is a 12 digit unique number. The first 11 digits are random and the last one digit is the checksum for the first 11 digits. Additionally, the Aadhar number never starts with 0 or 1, at least as of now.

Here is the regex pattern to include these rules.

Where,

Let’s try this regex against some sample numbers.

Output

The first two numbers are not 12 in length, so they are not valid. The third number contains the character “a” which is not allowed. The fourth number starts with 0 and fifth number starts with 1, so they are not valid as well.

Sometimes the number is entered in group of 3 having 4 digits each like ‘XXXX XXXX XXXX”. In this case, you might want to remove spaces from the original number before validating it with the regular expression. You can use the replace method to remove the spaces as given below.

Finally, if you are using JavaScript, jQuery, or any other front-end library, you can easily convert the regex pattern to Javascript as well.

This example is a part of the Java RegEx tutorial.

Please let me know your thoughts in the comments section below.

About the author

Leave a Reply

Your email address will not be published.