Search code examples
javascripthtmlchecksum

PESEL checksum validation(Javascript/HTML)


Im a student currently studying from home and im seriously stuck on a Checksum problem. The script is supposed to validate the PESEL(Polish equivilant of a social security number i think), Anyway the checksum works as follows for PESEL: 70051012347

PESEL:7,0,0,5,1,0,1,2,3,4 (7)

  (Multiply each Pesel number by its corresponding check number)

CHECK:1,3,7,9,1,3,7,9,1,3

 (Sum Each number)

SUM: + 7,0,0,45,1,0,7,18,3,12 =93

MOD: 93 MOD 10 = 3

10 - 3 = 7(last digit of pesel)

Where the MOD 10 doesn't equal 0, the result of sum%10 is subtracted from 10 and then matched with the final digit in the original number, if they match its good, if not its bad. All i need to have is a good or bad result.

I'm pretty sure I have all of this fine in my code and there's a simple solution i just cant see it. Any help at all would be massively appreciated.

<html>
<head>
<meta charset="utf-8">
<title>Pesel Checker</title>
<script type="text/javascript">

function peselgood(y)
{
    //sample PESEL's
    //type 1
    //70051012347
    //02070803628
    //07020803628
    //type 2
    //83102570819
    if (y.length == 11) 
    {

        var arr = [1,3,7,9,1,3,7,9,1,3];
        var sum = 0;

        //hold original number 
        var a = parseInt(y);

        //First 10 digits without check number and convert to array
        y = y.substring(0,9);
        y = parseInt(y);
        var arr1 = new Array(10);
        arr1 = y;

        //muliply pesel digits by checksum digits
        for (var i = 0; i < 10; i++)
        {
          sum += arr[i] * arr1[i];
        }

        sum = sum%10;
        if (sum !== 0)
        {
            sum = 10-sum;
            if(sum != a[10])
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    else
    {
        return false;
    }
}


function checkpesel()
{
    num = document.getElementById("peselfield").value
    if (peselgood(num))
    {
        document.getElementById("peselfield").style.background="#00ff00";
    }
    else
    {
        document.getElementById("peselfield").style.background="#ff6666";
    }
}

</script>


</head>
<body>
    <div>
    Check Sum Template
    <br/><br/>
    <form name="form">
        PESEL: 
        <input type="text" id="peselfield" value="70051012347" /> <button type="button" onclick="checkpesel()">Check</button>
        <br/><br/>
    </form> 
    </div>
    <br/><br/>
</body>
</html>

Solution

  • You have made a couple of mistakes. If you step through your code using a JavaScript debugger, you will find out exactly what goes wrong. The most important fact is, that you don't have to convert a string to an array of integers. JavaScript automatically understands when to convert a character to an integer. This is my solution:

        function peselgood(y)
        {
            if (y.length == 11)
            {
                var arr = [1,3,7,9,1,3,7,9,1,3];
                var sum = 0;
    
                //muliply pesel digits by checksum digits
                for (var i = 0; i < 10; i++)
                {
                    sum += arr[i] * y[i];
                }
    
                sum = sum%10 == 0 ? 0 : 10-sum%10;
                return sum == y[10];
            }
            else
            {
                return false;
            }
        }