In the digital age, Aadhaar, India’s biometric identification system, has become a critical component in various online and offline services. Ensuring the validity of Aadhaar numbers is crucial for data integrity and security. This article delves into the nuances of Aadhaar number validation through web applications, providing a step-by-step guide to implementing a robust validation mechanism using HTML, JavaScript, and jQuery.

1. Understanding Aadhaar Number Validation

Aadhaar numbers are 12-digit unique identifiers issued by the Indian government. To ensure that the numbers entered into a system are valid, we need to perform a series of checks:

    • Length Check: Aadhaar numbers must be exactly 12 digits long.
    • Format Check: The number should not start with 0 or 1 and should be composed solely of digits.
    • Checksum Validation: This is the most critical part, involving a checksum algorithm to verify the authenticity of the number.

2. The Role of HTML and JavaScript

To create an effective validation system, HTML is used for the user interface, while JavaScript handles the logic. In this example, we’ll use a combination of HTML, JavaScript, and jQuery to achieve a seamless user experience.

3. The HTML Structure

The HTML structure for our validation system is straightforward. It includes:

  • A text input field for entering the Aadhaar number.
  • A button to trigger the validation.
  • A div element to display error messages.

4. JavaScript for Validation

The JavaScript code is responsible for validating the Aadhaar number. It performs several tasks:

  • Converts the input string to an integer array in reverse order.
  • Uses predefined arrays for checksum calculations.
  • Checks the length and format of the input.
  • Displays appropriate error messages based on validation results.

How to Validate Aadhaar Card Number in JavaScript Regex

Complete  Script of Aadhaar Card Number Validation :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Aadhaar Number Validation</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        #error_app_aadhaar {
            color: red;
            display: none;
        }
    </style>
</head>
<body>
    <div>
        <label for="aadhaarNo">Aadhaar Number:</label>
        <input type="text" id="aadhaarNo" maxlength="12">
        <button onclick="validateAadhaar()">Validate</button>
        <div id="error_app_aadhaar"></div>
    </div>

    <script>
        // Validation data
        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]
        ];
        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]
        ];
        const inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];

        // Convert the input string to a reversed integer array
        function stringToReversedIntArray(inputtxt) {
            return inputtxt.split('').reverse().map(Number);
        }

        // Validate Aadhaar number using checksum algorithm
        function isValidAadhaar(inputtxt) {
            let c = 0;
            const myArray = stringToReversedIntArray(inputtxt);
            for (let i = 0; i < myArray.length; i++) {
                c = d[c][p[i % 8][myArray[i]]];
            }
            return c === 0;
        }

        function validateAadhaar() {
            // Get Aadhaar number from the input field
            var inputtxt = $('#aadhaarNo').val().trim();

            // Basic validation
            if (!inputtxt) {
                $('#error_app_aadhaar').text("Aadhaar number is mandatory.");
                $('#error_app_aadhaar').show();
                return false;
            }

            // Validate length and format
            const numbers = /^[2-9]\d{11}$/; // Must be 12 digits, not starting with 0 or 1
            if (inputtxt.length !== 12) {
                $('#error_app_aadhaar').text("Aadhaar number length should be 12 digits.");
                $('#error_app_aadhaar').show();
                return false;
            }
            if (!inputtxt.match(numbers)) {
                $('#error_app_aadhaar').text("Invalid Aadhaar number format.");
                $('#error_app_aadhaar').show();
                return false;
            }

            // Check for valid Aadhaar
            if (!isValidAadhaar(inputtxt)) {
                $('#error_app_aadhaar').text("Provided Aadhaar is invalid. Kindly enter a correct Aadhaar.");
                $('#error_app_aadhaar').show();
                return false;
            }

            // Hide error message if valid
            $('#error_app_aadhaar').hide();

            // Set encrypted values (assuming getfunctionalValue is defined elsewhere)
            $('#aadhaarEncrypt').val(getfunctionalValue(inputtxt));

            return true;
        }

        // Placeholder function for encryption (replace with actual logic if needed)
        function getfunctionalValue(value) {
            return value; // Replace with actual encryption logic if required
        }
    </script>

    <!-- Hidden fields for encrypted values -->
    <input type="hidden" id="aadhaarEncrypt">
</body>
</html>
5. Error Handling and User Feedback

Proper error handling is essential for a good user experience. In the provided code:

  • Mandatory Field Check: Ensures that the user has entered something.
  • Length and Format Validation: Ensures the number meets the required criteria.
  • Checksum Validation: Uses an algorithm to verify the authenticity.

6. Integrating Encryption

In real-world scenarios, Aadhaar numbers often need to be encrypted for security. The placeholder function getfunctionalValue in the code should be replaced with actual encryption logic, depending on the encryption requirements of your application.

7. Best Practices for Aadhaar Validation

  • Secure Transmission: Always use HTTPS to ensure secure transmission of Aadhaar numbers.
  • Data Privacy: Handle Aadhaar numbers with utmost care, adhering to data protection regulations.
  • Regular Updates: Keep validation logic updated according to any changes in Aadhaar number validation rules.

8. Conclusion

Implementing Aadhaar number validation is a crucial step in ensuring the integrity and security of user data in web applications. By following the guidelines and using the provided code, developers can create a robust validation mechanism that handles various validation scenarios effectively.

Incorporating this validation process into your web application not only improves user experience but also strengthens the security and reliability of your system. As technology and regulations evolve, staying informed and updating your validation logic will help maintain the effectiveness of your validation system.

Leave a Reply

Your email address will not be published. Required fields are marked *