南非身份证号码验证并获得年龄和性别 [英] South African ID Number Validate and Get Age and Gender

查看:334
本文介绍了南非身份证号码验证并获得年龄和性别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

感谢任何帮助,

Dawid

推荐答案

你可以使用Koenyn的正则表达式验证,不太确定输入中的一位数字(0-9?)如何代表性别,但基于你提供的这个工具和David Russell的,这是一项未经测试的尝试:

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 tool you provided and David Russell's Using Javascript to validate South African ID Numbers, here's an untested attempt:

UPDATE 1:
关注此主题后,
什么是南非身份证号码由?组成,我更新了我的实施包括性别和公民身份测试。

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.

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

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

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

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

这篇关于南非身份证号码验证并获得年龄和性别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆