Skip to content

Commit

Permalink
Fixed #264 -- Added django.core.validators.isValidIPAddress4. Thanks,…
Browse files Browse the repository at this point in the history
… Hugo and Simon

git-svn-id: http://code.djangoproject.com/svn/django/trunk@671 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Sep 23, 2005
1 parent 36fc73a commit 3d42660
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions django/core/formfields.py
Expand Up @@ -687,6 +687,17 @@ def isValidURL(self, field_data, all_data):
raise validators.CriticalValidationError, e.messages raise validators.CriticalValidationError, e.messages


class IPAddressField(TextField): class IPAddressField(TextField):
def __init__(self, field_name, length=15, maxlength=15, is_required=False, validator_list=[]):
validator_list = [self.isValidIPAddress] + validator_list
TextField.__init__(self, field_name, length=length, maxlength=maxlength,
is_required=is_required, validator_list=validator_list)

def isValidIPAddress(self, field_data, all_data):
try:
validators.isValidIPAddress4(field_data, all_data)
except validators.ValidationError, e:
raise validators.CriticalValidationError, e.messages

def html2python(data): def html2python(data):
return data or None return data or None
html2python = staticmethod(html2python) html2python = staticmethod(html2python)
Expand Down
8 changes: 8 additions & 0 deletions django/core/validators.py
Expand Up @@ -19,6 +19,7 @@
ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere)) ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere))
email_re = re.compile(r'^[-\w.+]+@\w[\w.-]+$') email_re = re.compile(r'^[-\w.+]+@\w[\w.-]+$')
integer_re = re.compile(r'^-?\d+$') integer_re = re.compile(r'^-?\d+$')
ip4_re = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE) phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
url_re = re.compile(r'^http://\S+$') url_re = re.compile(r'^http://\S+$')


Expand Down Expand Up @@ -85,6 +86,13 @@ def isCommaSeparatedEmailList(field_data, all_data):
except ValidationError: except ValidationError:
raise ValidationError, "Enter valid e-mail addresses separated by commas." raise ValidationError, "Enter valid e-mail addresses separated by commas."


def isValidIPAddress4(field_data, all_data):
if ip4_re.search(field_data):
valid_parts = [el for el in field_data.split('.') if 0 <= int(el) <= 255]
if len(valid_parts) == 4:
return
raise validators.ValidationError, "Please enter a valid IP address."

def isNotEmpty(field_data, all_data): def isNotEmpty(field_data, all_data):
if field_data.strip() == '': if field_data.strip() == '':
raise ValidationError, "Empty values are not allowed here." raise ValidationError, "Empty values are not allowed here."
Expand Down

0 comments on commit 3d42660

Please sign in to comment.