Validate swedish identity numbers
Add the GlobalPhone
nuget package to your app. For example, using Package Manager Console:
PM> Install-Package SweIdNum
First make sure to import the relevant namespaces
using SweIdNum;
using SweIdNum.Core;
Then you can parse swedish personal identity numbers
try{
var tolvansson = PersonalIdentityNumbers.Parse("121212-1212");
var birthdate = tolvansson.GetDate();
} catch (DoesNotMatchFormatException){
// ...
} catch (InvalidChecksumException){
// ...
}
If you don't want to handle potential exceptions then you use the TryParse method
if (PersonalIdentityNumbers.TryParse("121212-1212", out tolvansson))
{
// to stuff
}
First make sure to import the relevant namespaces. In the code below we import the module PersonalIdentityNumbers
for brevity.
open SweIdNum;
open SweIdNum.Core.PersonalIdentityNumbers
Note that the parse below will throw an exception if you feed it an invalid value
let tolvansson = parse "121212-1212"
let birthdate = getDate tolvansson
If you are unsure if it's correct and want to handle it in a more f#y way, then use tryParse
let maybeTolvansson = tryParse "121212-1212"
match maybeTolvansson with
| Choice1Of2 tolvansson ->
//...
| Choice2Of2 err ->
match err with
| DoesNotMatchFormat -> //...
| InvalidChecksum (expected,actual) -> //...