From c1093ca87fbcb57c47aeb323edacfadead859cb2 Mon Sep 17 00:00:00 2001 From: Honza Král Date: Sun, 5 Jul 2009 13:29:08 +0000 Subject: [soc2009/model-validation] SlugField and IPAddressField now use validators. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11191 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/validators.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'django/core/validators.py') diff --git a/django/core/validators.py b/django/core/validators.py index 1426c1c903..6758a387b0 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -22,6 +22,24 @@ def validate_email(value): if not email_re.search(smart_unicode(value)): raise ValidationError(_(u'Enter a valid e-mail address.'), code='invalid') +slug_re = re.compile(r'^[-\w]+$') + +def validate_slug(value): + if not slug_re.search(smart_unicode(value)): + raise ValidationError( + _(u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."), + code='invalid' + ) + +ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$') + +def validate_ipv4_address(value): + if not ipv4_re.search(smart_unicode(value)): + raise ValidationError( + _(u'Enter a valid IPv4 address.'), + code="invalid" + ) + class MaxValueValidator(object): def __init__(self, max_value): self.max_value = max_value -- cgit v1.3