Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. function validateRegon (regon) {
  2.  
  3. //REGON is a 9 or 14 digit number. Last digit is control digit from equation:
  4. // [ sum from 1 to (9 or 14) (x[i]*w[i]) ] mod 11; where x[i] is pointed NIP digit and w[i] is pointed digit
  5. //from [8 9 2 3 4 5 6 7] for 9 and [2 4 8 5 0 9 7 3 6 1 2 4 8] for 14 digits.
  6.  
  7. var n = regon.length;
  8. var w;
  9. var cd = 0; // Control digit (last digit)
  10. var isOnlyDigit = /^\d+$/.test(regon);
  11.  
  12. if ( n !==9 && n !== 14 && !isOnlyDigit) {
  13. console.log("Error");
  14. return false;
  15. }
  16.  
  17. if ( n === 9) {
  18. w = [8, 9, 2, 3, 4, 5, 6, 7];
  19. } else {
  20. w = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8];
  21. }
  22.  
  23. for (var i = 0; i<n-1; i++) {
  24. cd += w[i]*parseInt(regon.charAt(i));
  25. }
  26.  
  27. cd %= 11;
  28.  
  29. if ( cd === 10 ) {
  30. cd = 0;
  31. }
  32.  
  33. if ( cd !== parseInt(regon.charAt(n-1)) ) {
  34. console.log("Not valid");
  35. return false;
  36. } else {
  37. console.log("Valid!");
  38. }
  39.  
  40.  
  41.  
  42. }
  43.  
  44. // validateRegon("999999999"); // 9 digit
  45. // validateRegon("99999999999999"); // 14 digit
  46. // validateRegon("793769692"); // valid 9 digits Regon
  47. // validateRegon("63867185528390"); //valid 14 digits Regon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement