Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #269 -- Added MatchesRegularExpression validator. Thanks, Hugo!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@399 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3f2df5e7aa95e6cc78864a5cc452bb0c1d925a3e 1 parent cfbf261
Adrian Holovaty authored

Showing 1 changed file with 13 additions and 0 deletions. Show diff stats Hide diff stats

  1. 13  django/core/validators.py
13  django/core/validators.py
@@ -333,6 +333,19 @@ def __call__(self, field_data, all_data):
333 333
         if self.max_size is not None and len(field_data['content']) > self.max_size:
334 334
             raise ValidationError, self.max_error_message
335 335
 
  336
+class MatchesRegularExpression:
  337
+    """
  338
+    Checks that the field matches the given regular-expression. The regex
  339
+    should be in string format, not already compiled.
  340
+    """
  341
+    def __init__(self, regexp, error_message="The format for this field is wrong."):
  342
+        self.regexp = re.compile(regexp)
  343
+        self.error_message = error_message
  344
+
  345
+    def __call__(self, field_data, all_data):
  346
+        if not self.regexp.match(field_data):
  347
+            raise validators.ValidationError(self.error_message)
  348
+
336 349
 class URLMimeTypeCheck:
337 350
     "Checks that the provided URL points to a document with a listed mime type"
338 351
     class CouldNotRetrieve(ValidationError):

0 notes on commit 3f2df5e

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