iOS Danish CPR Validation with Swift

The main problem to be solved is to validate danish CPR numbers before it is send it to the server to get validated and check if the person really exist, why? well why waste network data and send something that is not valid creating a waste data and time. While all this can make be pre-check before data is sent.

This raised the issue of how to validate this danish CPR on Swift code for out iOS application…

Unknown

Well first the rules:

  • 10 digits long
  • 6 first are date of birth
    • day 00
    • month 00
    • year 0000
  • The last 4 numbers are for validation
    • first digit:
      • even decade 5-9
      • odd 0-4
    • the second and third is meaning less
    • the last digit:
      • odd for male
      • even for girls

 

The easier way is to split into 10 digits so we can later multiple it, the multiplication factors are predefined [4, 3, 2, 7, 6, 5, 4, 3, 2, 1] once each number is multiplied the sum of the result together and modulus with 11 will return 0 if the CPR is valid.

There maybe a easier or cleaner way to do this, so please leave the comment bellow :)

 
 
 

Leave a Reply

Your email address will not be published.