12 or 16 Digit Aadhar Number Validation using JavaScript

Last Reply one year ago By dharmendr

Posted one year ago

I want to do adhar card validation i.e. values in textbox must be 12 digits or 16 digits.

I am trying below javascript code :

           adhar = document.getElementById("txtaadhar").value;
           var adharcard = /^\d{12}$/;
           var adharsixteendigit = /^\d{16}$/;
           if (adhar != '') {
                if (!adhar.match(adharcard))
                {
                    alert("Invalid Aadhar Number");
                    return false;
                }
            }
            if (adhar != '')
            {
                if(!adhar.match(adharsixteendigit))
                {
                    alert("Invalid Aadhar Number");
                    return false;
                }
            }

what changs i should do to get my output

Thanks

Posted one year ago

Hi ashishk,

Refer below code.

HTML

<input type="text" id="txtAadhar" onblur="AadharValidate();" />
<script type="text/javascript">
    function AadharValidate() {
        var aadhar = document.getElementById("txtAadhar").value;
        var adharcardTwelveDigit = /^\d{12}$/;
        var adharSixteenDigit = /^\d{16}$/;
        if (aadhar != '') {
            if (aadhar.match(adharcardTwelveDigit)) {
                return true;
            }
            else if (aadhar.match(adharSixteenDigit)) {
                return true;
            }
            else {
                alert("Enter valid Aadhar Number");
                return false;
            }
        }
    }
</script>

Demo