javascript 南非身份证号码验证并获取年龄和性别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13344294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:30:20  来源:igfitidea点击:

South African ID Number Validate and Get Age and Gender

javascriptjqueryvalidation

提问by Dawid van der Hoven

I've researched this but none of the code I use seems to work. South African ID numbers contain date of birth and gender. All I want is it to pull in that information and verify it when their ID number is entered into an input field, preferably in jQuery or javascript

我已经研究过这个,但我使用的代码似乎都不起作用。南非身号码包含出生日期和性别。我想要的只是提取该信息并在将他们的 ID 号输入到输入字段时进行验证,最好是在 jQuery 或 javascript 中

Any help is appreciated,

任何帮助表示赞赏,

Dawid

大卫

回答by chridam

You could use Koenyn's regex validation, not so sure how a single-digit number (0-9?) from the input represents the gender but basing on this toolyou provided and David Russell's Using Javascript to validate South African ID Numbers, here's an untested attempt:

您可以使用 Koenyn 的正则表达式验证,不太确定输入中的一位数(0-9?)如何表示性别,但基于您提供的这个工具和 David Russell 的Using Javascript to validate South Africa ID Numbers,这是一个未经测试的试图:

UPDATE 1:After following this thread, What is a South African ID number made up of?, I updated my implementation to include the gender and citizenship tests.

更新 1:遵循此线程后,南非身号码由什么组成?,我更新了我的实施以包括性别和公民身份测试。

UPDATE 2:Forgot to wrap the month number increment id_month + 1within the date string fullDate, updating solution with Dawid's fix.

更新 2:忘记将月份数字增量包装id_month + 1在日期字符串中fullDate,使用 Dawid 的修复更新解决方案。

HTML Markup:

HTML 标记

<div id="error"></div>

<form id="idCheck">
    <p>Enter the ID Number: <input id="idnumber" /> </p>
    <p> <input type="submit" value="Check" /> </p>
</form>

<div id="result"> </div>

Javascript:

Javascript:

function Validate() {
    // first clear any left over error messages
    $('#error p').remove();

    // store the error div, to save typing
    var error = $('#error');

    var idNumber = $('#idnumber').val();


    // assume everything is correct and if it later turns out not to be, just set this to false
    var correct = true;

    //Ref: http://www.sadev.co.za/content/what-south-african-id-number-made
    // SA ID Number have to be 13 digits, so check the length
    if (idNumber.length != 13 || !isNumber(idNumber)) {
        error.append('<p>ID number does not appear to be authentic - input not a valid number</p>');
        correct = false;
    }

    // get first 6 digits as a valid date
    var tempDate = new Date(idNumber.substring(0, 2), idNumber.substring(2, 4) - 1, idNumber.substring(4, 6));

    var id_date = tempDate.getDate();
    var id_month = tempDate.getMonth();
    var id_year = tempDate.getFullYear();

    var fullDate = id_date + "-" + (id_month + 1) + "-" + id_year;

    if (!((tempDate.getYear() == idNumber.substring(0, 2)) && (id_month == idNumber.substring(2, 4) - 1) && (id_date == idNumber.substring(4, 6)))) {
        error.append('<p>ID number does not appear to be authentic - date part not valid</p>');
        correct = false;
    }

    // get the gender
    var genderCode = idNumber.substring(6, 10);
    var gender = parseInt(genderCode) < 5000 ? "Female" : "Male";

    // get country ID for citzenship
    var citzenship = parseInt(idNumber.substring(10, 11)) == 0 ? "Yes" : "No";

    // apply Luhn formula for check-digits
    var tempTotal = 0;
    var checkSum = 0;
    var multiplier = 1;
    for (var i = 0; i < 13; ++i) {
        tempTotal = parseInt(idNumber.charAt(i)) * multiplier;
        if (tempTotal > 9) {
            tempTotal = parseInt(tempTotal.toString().charAt(0)) + parseInt(tempTotal.toString().charAt(1));
        }
        checkSum = checkSum + tempTotal;
        multiplier = (multiplier % 2 == 0) ? 1 : 2;
    }
    if ((checkSum % 10) != 0) {
        error.append('<p>ID number does not appear to be authentic - check digit is not valid</p>');
        correct = false;
    };


    // if no error found, hide the error message
    if (correct) {
        error.css('display', 'none');

        // clear the result div
        $('#result').empty();
        // and put together a result message
        $('#result').append('<p>South African ID Number:   ' + idNumber + '</p><p>Birth Date:   ' + fullDate + '</p><p>Gender:  ' + gender + '</p><p>SA Citizen:  ' + citzenship + '</p>');
    }
    // otherwise, show the error
    else {
        error.css('display', 'block');
    }

    return false;
}

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

$('#idCheck').submit(Validate);

DEMO: http://jsfiddle.net/chridam/VSKNx/

演示http: //jsfiddle.net/chridam/VSKNx/

回答by Koenyn

this is the validation regex we us at our company:

这是我们公司的验证正则表达式:

string IdExpression = @"(?<Year>[0-9][0-9])(?<Month>([0][1-9])|([1][0-2]))(?<Day>([0-2][0-9])|([3][0-1]))(?<Gender>[0-9])(?<Series>[0-9]{3})(?<Citizenship>[0-9])(?<Uniform>[0-9])(?<Control>[0-9])";

as far as using regex, it's really simple http://www.w3schools.com/jsref/jsref_obj_regexp.asp

至于使用正则表达式,它真的很简单 http://www.w3schools.com/jsref/jsref_obj_regexp.asp

回答by 6dev6il6

There is a jQuery plugin that you can use. Check it out at http://www.verifyid.co.za/jqueryid

您可以使用一个 jQuery 插件。在http://www.verifyid.co.za/jqueryid 查看