Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3503 -- Added newforms UKPostcodeField in django.contrib.local…

…flavor.uk. Thanks for the patch, Jonathan Buchanan

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4527 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1d5e974a4f2e116a1530c24e6df54449b8ef3c98 1 parent c96d155
Adrian Holovaty authored February 15, 2007
19  django/contrib/localflavor/uk/forms.py
... ...
@@ -0,0 +1,19 @@
  1
+"""
  2
+UK-specific Form helpers
  3
+"""
  4
+
  5
+from django.newforms.fields import RegexField
  6
+from django.utils.translation import gettext
  7
+
  8
+class UKPostcodeField(RegexField):
  9
+    """
  10
+    A form field that validates its input is a UK postcode.
  11
+
  12
+    The regular expression used is sourced from the schema for British Standard
  13
+    BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
  14
+    """
  15
+    def __init__(self, *args, **kwargs):
  16
+        super(UKPostcodeField, self).__init__(r'^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW]) [0-9][ABD-HJLNP-UW-Z]{2})$',
  17
+            max_length=None, min_length=None,
  18
+            error_message=gettext(u'Enter a postcode. A space is required between the two postcode parts.'),
  19
+            *args, **kwargs)
0  localflavor/uk/__init__.py b/django/contrib/localflavor/uk/__init__.py
No changes.
44  tests/regressiontests/forms/tests.py
@@ -3327,6 +3327,50 @@
3327 3327
 >>> f.clean('')
3328 3328
 u''
3329 3329
 
  3330
+# UKPostcodeField ##############################################################
  3331
+
  3332
+UKPostcodeField validates that the data is a valid UK postcode.
  3333
+>>> from django.contrib.localflavor.uk.forms import UKPostcodeField
  3334
+>>> f = UKPostcodeField()
  3335
+>>> f.clean('BT32 4PX')
  3336
+u'BT32 4PX'
  3337
+>>> f.clean('GIR 0AA')
  3338
+u'GIR 0AA'
  3339
+>>> f.clean('BT324PX')
  3340
+Traceback (most recent call last):
  3341
+...
  3342
+ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  3343
+>>> f.clean('1NV 4L1D')
  3344
+Traceback (most recent call last):
  3345
+...
  3346
+ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  3347
+>>> f.clean(None)
  3348
+Traceback (most recent call last):
  3349
+...
  3350
+ValidationError: [u'This field is required.']
  3351
+>>> f.clean('')
  3352
+Traceback (most recent call last):
  3353
+...
  3354
+ValidationError: [u'This field is required.']
  3355
+
  3356
+>>> f = UKPostcodeField(required=False)
  3357
+>>> f.clean('BT32 4PX')
  3358
+u'BT32 4PX'
  3359
+>>> f.clean('GIR 0AA')
  3360
+u'GIR 0AA'
  3361
+>>> f.clean('1NV 4L1D')
  3362
+Traceback (most recent call last):
  3363
+...
  3364
+ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  3365
+>>> f.clean('BT324PX')
  3366
+Traceback (most recent call last):
  3367
+...
  3368
+ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']
  3369
+>>> f.clean(None)
  3370
+u''
  3371
+>>> f.clean('')
  3372
+u''
  3373
+
3330 3374
 #################################
3331 3375
 # Tests of underlying functions #
3332 3376
 #################################

0 notes on commit 1d5e974

Please sign in to comment.
Something went wrong with that request. Please try again.