【问题标题】:South African ID Number Validate and Get Age and Gender南非身份证号码验证并获取年龄和性别
【发布时间】:2012-11-12 13:08:37
【问题描述】:

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

感谢您的帮助,

大卫

【问题讨论】:

标签: javascript jquery validation


【解决方案1】:

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

更新 1: 在关注此线程 What is a South African ID number made up of? 之后,我更新了我的实现以包括性别和公民身份测试。

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

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:

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);

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

【讨论】:

  • 不客气!如果我有机会在这个solution 中通过@st-boost 实现优雅的校验位代码会很好
  • 嘿伙计,我发现了一个小问题......它返回的月份.. 似乎它从月份中减去一个然后尝试添加它,不幸的是而不是得到 7+1 = 8,它给了 7+1=71.. 我还在玩它,但是嘿,任何帮助都会很棒。
  • 抱歉修复了 - var fullDate = id_date + "-" + (id_month + 1) + "-" + id_year;
  • 啊,为此欢呼。忘记将月份数字增量包含在日期字符串中,就像您修复的方式一样。将用您的修复更新我的解决方案,谢谢。
  • 我正在使用您的代码,但在添加 ID 号 010101 时遇到了问题......它给出了出生年份 1901 而不是 2001 任何解决方法?
【解决方案2】:

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

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])";

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

【讨论】:

  • 所以...一个两位数表示年、月和日,然后是一位数(0-9?)表示性别(这是如何工作的?),三个数表示系列(在上下文中什么是“系列”?),以及公民身份、制服和控制权的个位数?
  • series 是该人当天注册的顺序。不知道其他人。
  • 正是我正在尝试的 ---> legalcity.net/Index.cfm?fuseaction=tools.idcheck
  • 谢谢,@Koenyn,您的回答对我有所帮助,但是,我注意到您的正则表达式允许生日为 00。我稍微更新了正则表达式的 部分:(?( [0][1-9])|([1-2][0-9])|([3][0-1]))
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多