使用javascript验证ID号

时间:2016-02-18 18:57:02

标签: javascript jquery html

我正在尝试验证南非ID号码。我不熟悉javascript。

我有以下代码:

  

HTML和Javascript

<html>

<head>
<script src="jquery-1.12.0.min.js"></script>
<title>the man</title>
<script>
    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);
</script>
</head>
  

身体:

<body>

<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>
</body>
</html>

不幸的是我没有收到任何错误输出。请协助

2 个答案:

答案 0 :(得分:0)

如果这是整个代码,那么在脚本之后你就会错过关闭头标记,我运行它并且只要显示不同的错误消息就可以了。

编辑 - 还添加了下面的编译代码,其中包含document.ready的简写。

<!DOCTYPE html>
<head>
<title>the man</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
  $(function() {

    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);
  });
</script>
</head>

<body>

<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>
</body>
</html>

答案 1 :(得分:0)

有两个建议的解决方案,它们都可以工作 首先从头部删除脚本。 (我猜你们都把</head>放在了不同的地方,这就是为什么你们其中一个提交正确而不是另一个提交的原因

<HTML>
<head>

<title>the man</title>
</head>
<body>

<script src="jquery-1.12.0.min.js"></script>
  <script> //your code</script>
<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>
</body>
</html>

另一个将所有代码包装在

$(document).ready(wrapper);
function wrapper(){
    function Validate() {
      //your code
        return false;
    }

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

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

为什么第一个解决方案有效?首先,我们渲染<head>,以便在运行代码时

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

无法附加,因为dom元素尚不存在。如果我们将代码放在执行中则会延迟 为什么第二种解决方案有效?我们等到所有页面都呈现出来;只有这样我们才能执行包含相同事件附件($('#idCheck').submit(Validate);)的函数。