validating the Aadhar card number in a application

后端 未结 5 1523
夕颜
夕颜 2021-01-31 11:33

we are developing a application which need to check whether user entering valid \"AADHAR\" number or not. i find some links and some \"apis\" but didn\'t meet final requirement

5条回答
  •  旧巷少年郎
    2021-01-31 11:49

    Use this javascript

    // multiplication table d
    var 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]
    ];
    
    // permutation table p
    var 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]
    ];
    
    // inverse table inv
    var inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];
    
    // converts string or number to an array and inverts it
    function invArray(array) {
    
        if (Object.prototype.toString.call(array) === "[object Number]") {
            array = String(array);
        }
    
        if (Object.prototype.toString.call(array) === "[object String]") {
            array = array.split("").map(Number);
        }
    
        return array.reverse();
    
    }
    
    // generates checksum
    function generate(array) {
    
        var c = 0;
        var invertedArray = invArray(array);
    
        for (var i = 0; i < invertedArray.length; i++) {
            c = d[c][p[((i + 1) % 8)][invertedArray[i]]];
        }
    
        return inv[c];
    }
    
    // validates checksum
    function validate(array) {
    
        var c = 0;
        var invertedArray = invArray(array);
    
        for (var i = 0; i < invertedArray.length; i++) {
            c = d[c][p[(i % 8)][invertedArray[i]]];
        }
    
        return (c === 0);
    }
    
     $(document).ready(function () {
      $('#UserForm').formValidation({
                        message: 'This value is not valid',
                        feedbackIcons: {
                            valid: 'glyphicon glyphicon-ok',
                            invalid: 'glyphicon glyphicon-remove',
                            validating: 'glyphicon glyphicon-refresh'
                        },
                        fields: {aadhaar_no: {
                                validators: {
                                    digits: {
                                        message: 'Please use numeric characters only.'
                                    },
                                    stringLength: {
                                        min: 12,
                                        max: 12,
                                        message: 'The aadhaar number must be 12 characters long'
                                    }, identical: {
                                        field: 'c_aadhaar_number',
                                        message: 'The aadhaar number and its confirm field are not the same'
                                    }, callback: {
                                        message: 'The input string is not a valid Aadhaar number.',
                                        callback: function (value, validator, $field) {
                                            return validate(value);
                                        }
                                    }
                                }
                            }
     });
                });

    Courtesy Wikipedia

提交回复
热议问题