Permalink
Browse files

Fixed #3847 -- Added validation support for Finnish social security n…

…umbers.

Thanks, karsu.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4868 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 51c7dad commit 63a629bb8dd55a7976d7a71a93966f5e0c05ca5c @malcolmt malcolmt committed Mar 30, 2007
Showing with 70 additions and 1 deletion.
  1. +26 −1 django/contrib/localflavor/fi/forms.py
  2. +44 −0 tests/regressiontests/forms/tests.py
@@ -2,8 +2,9 @@
FI-specific Form helpers
"""
+import re
from django.newforms import ValidationError
-from django.newforms.fields import RegexField, Select
+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
from django.utils.translation import gettext
class FIZipCodeField(RegexField):
@@ -20,3 +21,27 @@ class FIMunicipalitySelect(Select):
def __init__(self, attrs=None):
from fi_municipalities import MUNICIPALITY_CHOICES # relative import
super(FIMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
+
+class FISocialSecurityNumber(Field):
+ def clean(self, value):
+ super(FISocialSecurityNumber, self).clean(value)
+ if value in EMPTY_VALUES:
+ return u''
+
+ checkmarks = "0123456789ABCDEFHJKLMNPRSTUVWXY"
+ result = re.match(r"""^
+ (?P<date>([0-2]\d|3[01])
+ (0\d|1[012])
+ (\d{2}))
+ [A+-]
+ (?P<serial>(\d{3}))
+ (?P<chechsum>[%s])$""" % checkmarks, value, re.VERBOSE | re.IGNORECASE)
+ if not result:
+ raise ValidationError(gettext(u'Enter a valid Finnish social security number.'))
+ checksum = int(result.groupdict()['date'] + result.groupdict()['serial'])
+
+ if checkmarks[checksum % len(checkmarks)] == result.groupdict()['chechsum'].upper():
+ return u'%s' % value.upper()
+
+ raise ValidationError(gettext(u'Enter a valid Finnish social security number.'))
+
Oops, something went wrong. Retry.

0 comments on commit 63a629b

Please sign in to comment.