summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-08-04 14:42:01 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-08-04 14:42:01 +0000
commit3f2df5e7aa95e6cc78864a5cc452bb0c1d925a3e (patch)
tree77254330ad6a8bceb9748a5ab4660f908052f979
parentcfbf261709ffde0419a8aa75319e2f4ae4c29619 (diff)
Fixed #269 -- Added MatchesRegularExpression validator. Thanks, Hugo!
git-svn-id: http://code.djangoproject.com/svn/django/trunk@399 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/core/validators.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index c92d86e1d6..7e6f426278 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -333,6 +333,19 @@ class HasAllowableSize:
if self.max_size is not None and len(field_data['content']) > self.max_size:
raise ValidationError, self.max_error_message
+class MatchesRegularExpression:
+ """
+ Checks that the field matches the given regular-expression. The regex
+ should be in string format, not already compiled.
+ """
+ def __init__(self, regexp, error_message="The format for this field is wrong."):
+ self.regexp = re.compile(regexp)
+ self.error_message = error_message
+
+ def __call__(self, field_data, all_data):
+ if not self.regexp.match(field_data):
+ raise validators.ValidationError(self.error_message)
+
class URLMimeTypeCheck:
"Checks that the provided URL points to a document with a listed mime type"
class CouldNotRetrieve(ValidationError):