PESEL checksum validation(Javascript/HTML) -


im student studying home , im stuck on checksum problem. script supposed validate pesel(polish equivilant of social security number think), anyway checksum works follows pesel: 70051012347

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

  (multiply each pesel number 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 mod 10 doesn't equal 0, result of sum%10 subtracted 10 , matched final digit in original number, if match good, if not bad. need have or bad result.

i'm pretty sure have of fine in code , there's simple solution cant see it. @ 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 = parseint(y);          //first 10 digits without check number , convert array         y = y.substring(0,9);         y = parseint(y);         var arr1 = new array(10);         arr1 = y;          //muliply pesel digits checksum digits         (var = 0; < 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> 

you have made couple of mistakes. if step through code using javascript debugger, find out goes wrong. important fact is, don't have convert string array of integers. javascript automatically understands when convert character integer. 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 checksum digits             (var = 0; < 10; i++)             {                 sum += arr[i] * y[i];             }              sum = sum%10 == 0 ? 0 : 10-sum%10;             return sum == y[10];         }         else         {             return false;         }     } 

Comments

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -