Skip to content

Instantly share code, notes, and snippets.

@ivanpaulovich
Last active June 24, 2018 16:00
Show Gist options
  • Save ivanpaulovich/6c7776aaff93e29e21ec3e037c9df2e9 to your computer and use it in GitHub Desktop.
Save ivanpaulovich/6c7776aaff93e29e21ec3e037c9df2e9 to your computer and use it in GitHub Desktop.
A value object for the swedish Personnummer
public sealed class SSN
{
private string _text;
const string RegExForValidation = @"^\d{6,8}[-|(\s)]{0,1}\d{4}$";
public SSN(string text)
{
if (string.IsNullOrWhiteSpace(text))
throw new SSNShouldNotBeEmptyException("The 'SSN' field is required");
Regex regex = new Regex(RegExForValidation);
Match match = regex.Match(text);
if (!match.Success)
throw new InvalidSSNException("Invalid SSN format. Use YYMMDDNNNN.");
_text = text;
}
public static implicit operator SSN(string text)
{
return new SSN(text);
}
public static implicit operator string(SSN ssn)
{
return ssn._text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment